joshbrw / laravel-pagination-specification
A package for consistent pagination in Laravel.
Installs: 3 031
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7
- laravel/framework: 5.4.*|5.5.*
Requires (Dev)
- phpunit/phpunit: ^6.2
README
The main purpose of this package is to provide a consistent object that represents all of the main values you have to take into account when paginating (i.e. Current Page, Per Page, Appends for the URLs).
This object can then be passed around to methods and used for less verbose method parameters.
Installing
composer require joshbrw/laravel-pagination-specification
- Add the following Service Provider to your
config/app.php
underprovider
:Joshbrw\PaginationSpecification\PaginationSpecificationServiceProvider::class
- Optionally publish the config and change the default Per Page;
php artisan vendor:publish --provider="Joshbrw\PaginationSpecification\PaginationSpecificationServiceProvider"
Usage
The class is bound into the container under the Joshbrw\PaginationSpecification\PaginationSpecification
interface, and can be overwritten/decorated if required.
For example, injecting into a Controller method:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\View\View; use Joshbrw\PaginationSpecification\PaginationSpecification; class UserController extends Controller { public function index( Request $request, PaginationSpecification $paginationSpecification, UserRepository $userRepository ): View { // Reads the `per_page` and `page` values from the request $paginationSpecification->fromRequest($request); // Set how many items we want per page $paginationSpecification->setPerPage(30); // This can now be passed around to other methods, which can typehint it as a dependency return $userRepository->get($paginationSpecification); } }