实际上,在将 Laravel 从 v7.5.1 更新到 v7.6.2 后,我收到了这个错误
"message": "Trying to get property 'resource' of non-object",
"exception": "ErrorException",
"file": "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Http/Resources/Json/PaginatedResourceResponse.php",
"line": 29,
"trace": [
{
"file": "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Http/Resources/Json/PaginatedResourceResponse.php",
"line": 29,
"function": "handleError",
"class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
"type": "->"
},
{
"function": "Illuminate\\Http\\Resources\\Json\\{closure}",
"class": "Illuminate\\Http\\Resources\\Json\\PaginatedResourceResponse",
"type": "->"
},
{
"file": "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Support/Collection.php",
"line": 638,
"function": "array_map"
},
{
"file": "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php",
"line": 23,
"function": "map",
"class": "Illuminate\\Support\\Collection",
"type": "->"
},
这就是导致错误的原因:
$products = Product::findMany($array)->paginate(10);
//dump($products);
/*Create ResourceCollection*/
return new ProductResourceCollection($products);
转储返回:
------------ -----------------------------------------------
date Thu, 16 Apr 2020 08:53:53 +0000
controller "ProductApiController"
source ProductApiController.php on line 56
file app/Http/Controllers/ProductApiController.php
------------ -----------------------------------------------
Illuminate\Pagination\LengthAwarePaginator^ {#300
#total: 1
#lastPage: 1
#items: Illuminate\Database\Eloquent\Collection^ {#319
#items: array:1 [
0 => App\Product^ {#400
#casts: array:9 [
"category" => "object"
]
#connection: null
#table: "products"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:21 [
"id" => "1"
"title" => "Test"
"category" => "{"id":1,"title":"Test","slug":null}"
]
#original: array:21 [
"id" => "1"
"title" => "Test"
"category" => "{"id":1,"title":"Test","slug":null}"
]
}
]
}
#perPage: 10
#currentPage: 1
#path: "http://example.org/api/product/pluck/1,2,3"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: array:2 [
"path" => "http://example.org/api/product/pluck/1,2,3"
"pageName" => "page"
]
}
这是资源:
public function toArray($request)
{
return [
'current_page' => $this->resource->currentPage(),
'first_page_url' => $this->resource->url(1),
'from' => $this->resource->firstItem(),
'last_page' => $this->resource->lastPage(),
'last_page_url' => $this->resource->url($this->lastPage()),
'next_page_url' => $this->resource->nextPageUrl(),
'per_page' => $this->resource->perPage(),
'prev_page_url' => $this->resource->previousPageUrl(),
'to' => $this->resource->lastItem(),
'total' => $this->resource->total(),
'data' => $this->collection->transform(function ($product
) {
return [
'id' => $product->id,
'title' => $product->title,
'category' => $product->category,
];
}),
];
}
如果我评论'data'=> $this->collection->transform(function ($product) {
错误不会出现......但是......好吧,那么应用程序不起作用;-)
我的问题
所以我的问题是:这种修改数据的方式是否不正确,我该如何解决?