danbovey / laravel-linkheader-paginator by danbovey

Laravel Paginator that moves pagination info to the HTTP LinkHeader
3,433
3
2
Package Data
Maintainer Username: danbovey
Maintainer Contact: dan@danbovey.uk (Dan Bovey)
Package Create Date: 2016-11-06
Package Last Update: 2016-11-08
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-17 03:03:39
Package Statistics
Total Downloads: 3,433
Monthly Downloads: 99
Daily Downloads: 0
Total Stars: 3
Total Watchers: 2
Total Forks: 1
Total Open Issues: 2

Laravel LinkHeader Paginator

Travis Packagist Downloads Version License

A custom Laravel/Lumen Paginator that uses the Link header (RFC 5988) 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.

$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 methodsimplePaginate, and implement the skip/take logic yourself.

$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();