Laravel api资源和许可证问题
我正在使用雄辩的API资源制作API 这是我的文章资源:Laravel api资源和许可证问题,laravel,api,eloquent,Laravel,Api,Eloquent,我正在使用雄辩的API资源制作API 这是我的文章资源: class Article extends JsonResource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) {
class Article extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'slug' => $this->slug,
'name' => $this->name,
'comments' => $this->when($this->showComments(), function () {
$comments = config('eblogger.models.comment')::where([
'commentable_type' => get_class($this),
'commentable_id' => $this->id,
'parent_id' => 0,
])->orderBy('created_at', 'desc')->get();
$paginator = makePaginationCollection($comments, route('blog.comments'));
return CommentResource::collection($paginator);
}),
];
}
}
public function toArray($request)
{
return [
// ...
'comments' => $this->when($this->showComments(), function() {
return new CommentCollection(
$this->comments()
->orderBy('created_at', 'desc')
->paginate(config('settings.items_by_pages'))
->withPath(route('blog.comments'))
);
})
];
}
这是一篇有评论的文章。
我想对我的评论进行分页,所以我调用了一个自定义帮助程序
function makePaginationCollection($collection, $path)
{
$request = request();
$page = request('page', 1);
$perPage = config('settings.items_by_pages');
$paginate = new \Illuminate\Pagination\LengthAwarePaginator(
$collection->forPage($page, $perPage),
$collection->count(),
$perPage,
$page,
['path' => $path]
);
return $paginate;
}
更新:这是我的资源集合
class CommentCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => $this->collection,
];
}
}
但是当我检查devtools中的响应时,我看到
我试着用CommentCollection课程,但没有成功
你有什么想法吗
谢谢
我的解决方案
最后,感谢@Rwd,我找到了这个解决方案,但我认为可以做得更好:
这是我的文章资源:
class Article extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'slug' => $this->slug,
'name' => $this->name,
'comments' => $this->when($this->showComments(), function () {
$comments = config('eblogger.models.comment')::where([
'commentable_type' => get_class($this),
'commentable_id' => $this->id,
'parent_id' => 0,
])->orderBy('created_at', 'desc')->get();
$paginator = makePaginationCollection($comments, route('blog.comments'));
return CommentResource::collection($paginator);
}),
];
}
}
public function toArray($request)
{
return [
// ...
'comments' => $this->when($this->showComments(), function() {
return new CommentCollection(
$this->comments()
->orderBy('created_at', 'desc')
->paginate(config('settings.items_by_pages'))
->withPath(route('blog.comments'))
);
})
];
}
这是我的资源集:
public function toArray($request)
{
return [
'data' => $this->collection,
'links' => $this->resource,
'meta' => $this->resource
];
}
您是否对
文章
模型有评论
关系(假设文章
也是模型的名称)?是。如果我只返回$paginator,我可以看到分页信息,但数据数组是空的。我想你误解了我的问题,我的意思是你有类似于return$this->morphMany(Comment::class,'commentable')的东西吗代码>在模型中(不是资源)?是的,我有:返回$this->morphMany(Comment::class,'commentable')->where('parent_id','=',0);是的,这更好,因为我可以返回数据,但我没有分页链接和元数据。您是否在文章
模型上有注释
关系(假设文章
也是模型的名称)?是的。如果我只返回$paginator,我可以看到分页信息,但数据数组是空的。我想你误解了我的问题,我的意思是你有类似于return$this->morphMany(Comment::class,'commentable')的东西吗代码>在模型中(不是资源)?是的,我有:返回$this->morphMany(Comment::class,'commentable')->where('parent_id','=',0);是的,这更好,因为我可以返回数据,但我松散的分页链接和元。