Replies: 4 comments 5 replies
-
Hi, I was reading the laravel docs and it appears resource collections already work with pagination. although I haven't used it, but does that work for what you are trying to accomplish? |
Beta Was this translation helpful? Give feedback.
-
Why not simply extend the ResourceCollection to your needs? IMO pagination information should not be in That's what I do: class AbstractJsonApiCollection extends ResourceCollection
{
public function paginationInformation($request, $paginated, $default): array
{
return [
'links' => $default['links'],
'meta' => [
'firstItemNumber' => $default['meta']['from'],
'lastItemNumber' => $default['meta']['to'],
'totalItemCount' => $default['meta']['total'],
'itemsPerPage' => $default['meta']['per_page'],
'currentPageNumber' => $default['meta']['current_page'],
'lastPageNumber' => $default['meta']['last_page']
]
];
}
}
``´
this way, you only need to extend your Collection from the Abstract one. At least no duplicate code.
```php
class MyCollection extends AbstractJsonApiCollection {} that's all |
Beta Was this translation helpful? Give feedback.
-
@hsmfawaz you can have a look here as an alternative: https://github.com/macropay-solutions/laravel-crud-wizard-free It supports length aware paginator, simple paginator and cursor. You also can overwrite the controller function https://github.com/macropay-solutions/laravel-crud-wizard-free/blob/1d3a509651ad6be1b2742445ffe9181187b8dce0/src/Http/Controllers/ResourceControllerTrait.php#L275 to format the paginator fields in the response. |
Beta Was this translation helpful? Give feedback.
-
I do like this -
|
Beta Was this translation helpful? Give feedback.
-
Hello Laravel Team ,
I wanna introduce to you my idea to enhance JsonResource class as the following
The problem:
For the last 3 years ,
I was working on providing API for mobile applications using Laravel and Lumen
using laravel JsonResource helped me a lot to make a good api responses, until now no problem,
The problem come with Pagination when i want to paginate a JsonResource ,I create a JsonCollection to handle the pagination.
at the end of every application ,I end up with a ton of JsonCollection has the same functionality and 99% same code only the resource name is different
Something like this:
The solution:
I tried many solutions to eliminate the unneeded collections files
My last solution was implementing a HasPagination Trait and use it in JsonResource
//HasPagination Trait
Now i can paginate JsonResource like
I will enhance the trait to be able to paginate cursor pagination and simple pagination,
Maybe enhancing the static collection method in the JsonResource to handle the pagination along side the collection
too many enhancements in my head to handle the pagination functionality with the jsonResource
What is your opinion about my idea ?
Thanks so much for your time.
Beta Was this translation helpful? Give feedback.
All reactions