Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

untymage's avatar

Where to put 'comments_count' in API Resource ?

    "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']

0 likes
10 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@untymage and have you tried it this way?

return [
    'data' => [
         CommentsResource::collection($this->collection),
         'comments_count' => $this->collection->first()->commentable->comments_count
    ],
];

Or try to push it to the collection returned by the resource:

CommentsResource::collection($this->collection)
    ->push('comments_count',
        $this->collection->first()->commentable->comments_count),
untymage's avatar

@nakov I realized that when using CommentsResource inside of data key the output have extra 0 key on each array:

[{
    "data": {
        "0": [
            {
                "username": "Prof. Millie Bailey",
                "body": "Explicabo laudantium ut
            }
        ],
        "comments_count": 1
    }
}]

Why is that? how to remove it

Nakov's avatar

@untymage because CommentResource returns a collection, and now the data is an array as well.

Have you tried both versions that I've shared?

What about using this:

return [
    'data' => CommentsResource::collection($this->collection)->push('comments_count', $this->collection->first()->commentable->comments_count),
];  
untymage's avatar

Ok, But look at this:

image

I have two extra key that i mark in the image, first is for data key and second is around comments array

$json[0]['data'] and $json[0]['data'][0][0] I dont like this mess xD

Nakov's avatar

I don't like that neither but do you have just this now

public function toArray($request)
{
    return [
        'data' => CommentsResource::collection($this->collection)->push('comments_count', $this->collection->first()->commentable->comments_count),
];  
}

and it produces that weird result?

untymage's avatar

Done, Now is better :

image

If we could remove that 0 around the data key ......... :d

Nakov's avatar

@untymage now the problem is within your CommentResource.. maybe you just need to use that one.. or try this instead

public function toArray($request)
{
    return CommentsResource::collection($this->collection)->push('comments_count', $this->collection->first()->commentable->comments_count);
}
untymage's avatar

One thing, push method takes one param, Do you meanput in all your replies right?

Nakov's avatar

Then you won't need the extra resource that you use, and you can add in each item of the CommentResource.. But that doesn't make sense. As you need the total count once..

Please or to participate in this conversation.