andyandy's avatar

API Resources - Resource Collections - Pagination meta data disappear

I'm studying this https://laravel.com/docs/5.7/eloquent-resources

This code will return 5 articles + pagination meta data (current page, last_page etc.)

        $publications = Publication::paginate(5);
        $publications = PublicationResource::collection($publications);
        return $publications;

Great.

Now I will generate resource for controlling collections of models. And allows me add some extra metadata into response. (author, version, date)

        $publications = Publication::paginate(5);
        $publications = new PublicationCollection(PublicationResource::collection($publications));
        return $publications;

Which works. I will get 5 articles + my extra meta data.

BUT pagination data are gone. And no matter what I try I can't get pagination meta data back.

0 likes
2 replies
mabdullahsari's avatar
Level 16

Well, if you are going to use a dedicated resource collection class, then you do not have to use the static collection method which returns an AnonymousResourceCollection. At the moment, you are wrapping a collection inside another collection.

It's one or the other, not both.

Please or to participate in this conversation.