Package Data | |
---|---|
Maintainer Username: | dvsouto |
Maintainer Contact: | davi.souto@gmail.com (Davi Souto) |
Package Create Date: | 2019-07-18 |
Package Last Update: | 2022-12-20 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2025-02-06 15:05:27 |
Package Statistics | |
---|---|
Total Downloads: | 1,162 |
Monthly Downloads: | 8 |
Daily Downloads: | 1 |
Total Stars: | 2 |
Total Watchers: | 2 |
Total Forks: | 1 |
Total Open Issues: | 0 |
In vanilla Laravel application exists the method paginate
in Query Builder (https://laravel.com/docs/5.8/pagination#paginating-eloquent-results), that returns the results formated to front pagination.
This package adds a jsonPaginate
method to the Eloquent query builder that listens the results in json format to use in API and show data in others applications.
You can install the package via composer:
composer require dvsouto/laravel-json-paginate
In Laravel 5.5 and above the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php
file:
'providers' => [
...
Bitnary\JsonPaginate\JsonPaginateServiceProvider:class, // Bitnary JsonPaginate for o Eloquent
];
In Lumen you need to load de service provider on bootstrap/app.php
file:
...
$app->register(Bitnary\JsonPaginate\JsonPaginateServiceProvider:class); // Bitnary JsonPaginate for o Eloquent
To paginate the results prepared to return from API, simply call the jsonPaginate
method.
YourModel::jsonPaginate();
Of course you may still use all the builder methods you know:
YourModel::where('my_field', 'myValue')->jsonPaginate();
By default the maximum page size is set to 100. You can change this number passing the value to jsonPaginate
.
You also pass the each side parameter to method for generate the page keys.
$per_page = 10; // 10 Results per page
$each_side = 3; // 3 buttons on each side
YourModel::jsonPaginate($per_page, $each_side);
By default the maximum page size is set to 100. You can change this number passing the value to jsonPaginate
.
$maxResults = 10;
YourModel::jsonPaginate($maxResults);
This return an array containing thats parameters:
{
"data": [ // (object) Results for this page
{
"id": 1,
"name": "Davi Souto"
},
{
"id": 2,
"name": "Lorem Ipsum"
},
{
"id": 3,
"name": "Sit Amet"
},
...
],
"paginator": {
"current_page": 1, // (int) Current page number
"prev_page": 1, // (int) Previous page number
"next_page": 2, // (int) Next page number
"first_page": 1, // (int) The first page (always 1),
"is_first_page": true, // (boolean) Is in first page ?
"last_page": 25, // (int) Last page number
"is_last_page": false, // (boolean) Is in last page ?
"page_keys": [ // (array) Contains the list of pages to display in front
1,
2,
3,
4,
],
"from_item": 1, // (int) Results for this page starts from this item
"to_item": 100, // (int) Results for this page ends in this item
"total_items": 100, // (int) Total of existent itens
"per_page": 10, // (int) Results for this page ends in this item
"display_items": // (int) Total of itens in this page
}
}
If you discover any security related issues, please email davi.souto@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.