UAVova's avatar

ResourceCollection doesn't include pagination links.

CommentsCollection

class CommentsCollection extends ResourceCollection
{
    public function toArray($request)
    {
        return [
            'data' => $this->collection
        ];
    }
}

CommentsController

public function index()
{
    $post = Post::find(1);
    return ['post'=> $post, 'comments' => new CommentsCollection(Comment::paginate(1))];
}

Response

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

So, this happens when resource with use of ::collection method or even ResourceCollection returned as a part of the array.

If we gonna remove array and return pure resource like return new CommentsCollection(Comment::paginate(1)), everything is going to work fine and response will include meta and links.

Why does API Resource (using collection method or ResourceCollection) doesn't include pagination information when returned in array?

0 likes
0 replies

Please or to participate in this conversation.