grindwebstudio's avatar

How to add pagination meta when combining Collection with Resource in API Resource

Hi there all !

Building a API in here and using the new API Resource options.

However I need this to work:

$users = User::paginate(10);

return new UserCollection(UserResource::collection($users));

This will not include any meta or links data for that pagination. However if I run:

$users = User::paginate(10);

return new UserCollection($users);

It does include meta data for the pagination, but that way I cant modify the resources. Seems pretty simple but cant find a working solution so far.

0 likes
4 replies
rumm.an's avatar

What your UserCollection looks like?

grindwebstudio's avatar

public function toArray($request) { //TODO resolve pagination data info return [ 'data' => $this->collection ]; }

tokoiwesley's avatar

You'll need to use UserResource to modify the resource and do the following in order to return meta data for pagination;

$users = UserResource::collection(User::paginate(10));

return response()->json($users->resource);
drs997's avatar

hey did you find a solution to this?

Please or to participate in this conversation.