【发布时间】:2019-12-20 16:49:51
【问题描述】:
我正在尝试构建资源集合的数据输出。随着分页我得到:
未定义属性:Illuminate\Pagination\LengthAwarePaginator::$id
return new PostCollection(Post::latest()->get()); 在 PostControllers 索引中看到,我得到:
此集合实例上不存在属性 [id]。
后控制器
public function index()
{
//return PostResource::collection(Post::latest()->paginate(1));
return new PostCollection(Post::latest()->paginate(1));
}
PostCollection 资源
class PostCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
//return parent::toArray($request);
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
'slug' => $this->slug,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
注释掉的PostResource::collection() 在相同的代码下工作得很好。如何构建我的 PostCollection 资源的数据?
【问题讨论】:
标签: laravel