【发布时间】:2020-01-24 19:02:27
【问题描述】:
评论集合
class CommentsCollection extends ResourceCollection
{
public function toArray($request)
{
return [
'data' => $this->collection
];
}
}
评论控制器
public function index()
{
$post = Post::find(1);
return ['post'=> $post, 'comments' => new CommentsCollection(Comment::paginate(1))];
}
回应
"comments": {
"data": [
{
"id": 1,
"content": "First comment",
"post_id": 6,
"account_id": 1,
"created_at": "2018-03-07 02:50:33",
"updated_at": "2018-03-07 02:50:34"
}
]
}
当资源使用 ::collection 方法甚至 ResourceCollection 作为数组的一部分返回时,会发生这种情况。
如果我们要删除数组并返回纯集合:
return new CommentsCollection(Comment::paginate(1))
一切正常,响应将包括元和链接。
为什么API Resource(使用collection方法或ResourceCollection)在数组中返回时不包含分页信息?
【问题讨论】:
标签: laravel laravel-5.5