"data": [
{
"username": "Sigurd Homenick",
"body": "Et repudiandae corporis quam modi.
},
{
"username": "Homenick",
"body": "Et r213epudiandae corporis quam modi.
}
],
What is appropriate position of comments_count in API Resources ? I would like to put in direct key of data wrapper and access it on view like $json['data']['comments_count']
But i have two problem,
If i use collection method i can not add any additional meta data into json,
So i tried CommnetCollection instead in order to add aditional meta data
public function show(Thread $thread)
{
return new CommentsCollection(
$thread->comments
);
}
class CommentsCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => CommentsResource::collection($this->collection),
'comments_count' => $this->collection->first()->commentable->comments_count
];
}
}
But in this scenario comments_count is outside of data wrapper and if add it to CommentResource it shows comments_count on every comment array i want it included in data key but not in all array inside of it just like this : $json['data']['comments_count']