@syntaxerron to return meta data with the resource you need to paginate items, not get all https://laravel.com/docs/8.x/eloquent-resources#pagination
How to include meta and links in Laravel API Resource Collection?
I am building an API for an SPA and I used Laravel API Resources with pagination. I have been using it on my previous projects but on the latest update, the meta data and links are gone. I am using the latest version of Laravel 7.
I used this on my previous projects when retrieving a resource collection:
UserResource::collection(User::all());
which returns:
current_page: 1
data: [{…}, __ob__: Observer]
first_page_url: "http://127.0.0.1:8000/api/v1/users?page=1"
from: 1
last_page: 1
last_page_url: "http://127.0.0.1:8000/api/v1/users?page=1"
next_page_url: null
path: "http://127.0.0.1:8000/api/v1/users"
per_page: "10"
prev_page_url: null
to: 1
total: 1
But now, there are no more links and meta data returned with the collection just like the previous implementation:
data: [{…}]
The documentation recommends to use dedicated resource to represent the collection:
php artisan make:resource UserCollection
and use:
return new UserCollection(User::all());
I was a bit confused with the documentation. Does anybody know the proper way of returning api resource collection just like before with the links and meta data using the new recommended way from the documentation?
Please or to participate in this conversation.