https://github.com/danbovey/laravel-linkheader-paginator
✉️ Laravel Paginator that removes the envelope and puts pagination info in the HTTP Link header!
https://github.com/danbovey/laravel-linkheader-paginator
laravel laravel-pagination link-header
Last synced: 5 months ago
JSON representation
✉️ Laravel Paginator that removes the envelope and puts pagination info in the HTTP Link header!
- Host: GitHub
- URL: https://github.com/danbovey/laravel-linkheader-paginator
- Owner: danbovey
- License: mit
- Created: 2016-11-06T15:58:11.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-08T14:02:24.000Z (over 9 years ago)
- Last Synced: 2024-08-10T20:00:06.054Z (almost 2 years ago)
- Topics: laravel, laravel-pagination, link-header
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel LinkHeader Paginator
[](https://travis-ci.org/danbovey/laravel-linkheader-paginator)
[](https://packagist.org/packages/danbovey/laravel-linkheader-paginator)
[](https://packagist.org/packages/danbovey/laravel-linkheader-paginator)
[](https://raw.githubusercontent.com/danbovey/laravel-linkheader-paginator/master/LICENSE)
A custom Laravel/Lumen Paginator that uses the [Link header (RFC 5988)](https://tools.ietf.org/html/rfc5988) to send pagination info in the response. Removes the envelope around `data`!
Adds a method called `toResponse` that returns a JSON response with headers. The `getHeaders` method exists if you need different response data.
## Installation
```
$ composer require danbovey/laravel-linkheader-paginator
```
## Usage
Create the pagination with the Eloquent/DB Builder and pass it to the `LengthAwarePaginator`.
```php
$items = User::where('active', 1)->paginate(20);
$paginator = new LengthAwarePaginator($items);
return $paginator->toResponse();
```
**"Simple Pagination"**
The simple paginator does not need to know the total number of items in the result set; however, because of this, the class does not return the URI of the last page.
Ironically, the simple paginator is more work using this library. To save on queries you should skip using the method`simplePaginate`, and implement the `skip`/`take` logic yourself.
```php
$page = $request->get('page');
$perPage = 20;
// Take one more than needed to see if there is a next page
$users = User::skip(($page - 1) * $perPage)
->take($perPage + 1);
$paginator = new Paginator($simple, $items);
return $paginator->toResponse();
```