A helper package to paginate Laravel collections.
Learn the idea behind this package:
- Read on my Medium blog: How to Paginate Collection
- or watch it on Youtube
Follow us on Youtube: Acadea.io
You can install the package via composer:
composer require acadea/collection-paginator
$collection = collect([1,2,3,4,5,6,7,8]);
$pageSize = 4;
$paginated = \Acadea\CollectionPaginator\CollectionPaginator::paginate($collection, $pageSize);
// ..
// in resource controller, returning as an api response
return new \Illuminate\Http\JsonResponse($paginated);
Expects:
{
"current_page": 1,
"data": [
1,
2,
3,
4
],
"first_page_url": "http://localhost:8000/api/fruits?page=1",
"from": 1,
"last_page": 2,
"last_page_url": "http://localhost:8000/api/fruits?page=2",
"links": [
{
"url": null,
"label": "Previous",
"active": false
},
{
"url": "http://localhost:8000/api/fruits?page=1",
"label": 1,
"active": true
},
{
"url": "http://localhost:8000/api/fruits?page=2",
"label": 2,
"active": false
},
{
"url": "http://localhost:8000/api/fruits?page=2",
"label": "Next",
"active": false
}
],
"next_page_url": "http://localhost:8000/api/fruits?page=2",
"path": "http://localhost:8000/api/fruits",
"per_page": 4,
"prev_page_url": null,
"to": 4,
"total": 8
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.