Php 使用keyby收集方法检索有说服力的api资源
我有一个端点Php 使用keyby收集方法检索有说服力的api资源,php,laravel,laravel-5,eloquent,laravel-resource,Php,Laravel,Laravel 5,Eloquent,Laravel Resource,我有一个端点 users/{user} 现在在用户资源中,我想返回 public function toArray($request) { // return parent::toArray($request); return [ 'id' => $this->id, 'name' => $this->name, //
users/{user}
现在在用户资源中,我想返回
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'name' => $this->name,
// 'comments' => $this->post->comments->keyBy('post_id')
'comments' => new CommentCollection($this->post->comments->keyBy->post_id)
];
}
评论收集类
public function toArray($request)
{
// return parent::toArray($request);
return [
'data' => $this->collection->transform(function($comment){
return [
'id' => $comment->id,
'comment' => $comment->comment,
];
}),
];
}
<?php
namespace App\Http\Resources\Collection;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CommentGroupCollection extends ResourceCollection
{
public $collects = 'App\Http\Resources\Collection\CommentCollection';
public $preserveKeys = true;
public function toArray($request)
{
return $this->collection;
}
}
<?php
namespace App\Http\Resources\Collection;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CommentCollection extends ResourceCollection
{
public $collects = 'App\Http\Resources\Comment';
public $preserveKeys = true;
public function toArray($request)
{
return $this->collection;
}
}
and then
new CommentGroupCollection($comments->groupBy('post_id')),
但是结果将不包括作为键的post\u id
,如何使其返回具有键post\u id
的注释集合
更新
use App\models\Post;
use App\Http\Resources\Postas PostResource;
Route::get('/posts', function () {
return PostResource::collection(Post::all()->keyBy->slug);
});
这是正确的工作,但如果我将用户资源内的post集合用作关系,它就不工作了!这就是我在评论集中的要求。就像这样:
公共功能待命($request)
{
//返回父::toArray($request);
返回[
'id'=>this->id,
“名称”=>$this->name,
//'comments'=>this->post->comments->keyBy('post\u id'))
'comments'=>newcommentcollection($this->post->comments)->keyBy('post\u id'))
];
}
我做了什么,创建了另一个ResourceGroupCollection类
public function toArray($request)
{
// return parent::toArray($request);
return [
'data' => $this->collection->transform(function($comment){
return [
'id' => $comment->id,
'comment' => $comment->comment,
];
}),
];
}
<?php
namespace App\Http\Resources\Collection;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CommentGroupCollection extends ResourceCollection
{
public $collects = 'App\Http\Resources\Collection\CommentCollection';
public $preserveKeys = true;
public function toArray($request)
{
return $this->collection;
}
}
<?php
namespace App\Http\Resources\Collection;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CommentCollection extends ResourceCollection
{
public $collects = 'App\Http\Resources\Comment';
public $preserveKeys = true;
public function toArray($request)
{
return $this->collection;
}
}
and then
new CommentGroupCollection($comments->groupBy('post_id')),
您可以看到,我有确切的代码,但它不起作用,这就是我发布问题的原因。您可以发布CommentCollection类吗?不是toArray函数(),它不会在您的案例中使用,它是如何构造的?你有一个post_id栏吗?你能添加(新评论集($this->post->comments))?我有一些关于我的问题的更新,你能调查一下吗,也许我们会有一些线索。我找到了一个解决方案,并发布了,也许你想检查一下。