Project, pagination is often used.
Laravel also comes with pagination.
But sometimes require minor modifications to meet their needs.
An ordinary paging
1, the controller, with the paginate () method.
$users = DB::table('users')->paginate(15);
Or simply paging
$users = DB::table('users')->simplePaginate(15);
2, blade template, you can directly use data query results
{{ $users->links() }}、{{ $users->render() }}
Paging style comes bootstamp
3, custom paging URI
$users->withPath('custom/url');
4, an additional parameter to the tab
$users->appends(['sort' => 'votes'])->links()
Second, the custom paging
1, custom page template
php artisan vendor:publish --tag=laravel-pagination
Automatically creates pagination / directory in the resources / views directory
It will own template copy tab in the above directory.
2, modify the template
For example, modify the display the number of links, page content.
3, call custom template
$paginator->links('view.name')
links parameters as a template path
Third, the collection of pages
Many times the query results need to deal with Collection after paging, and Laravel is not supported.
The following minor modifications, to achieve the above requirements
1, a collection of processes the query results
$users = DB::table('users')
->get()
->each(function($item, $key){
$item->total = 11;
})->paginate(15);
After the above treatment, you will find page disappeared.
2, paging service provider to join in
In the app / Providers / AppServiceProvider.php file,
The following classes introducing head
use Illuminate\Pagination\Paginator; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection;
Add the following code boot method
if (!Collection::hasMacro('paginate')) { Collection::macro('paginate', function ($perPage = 15, $page = null, $options = []) { $page = $page ?: (Paginator::resolveCurrentPage() ?: 1); return (new LengthAwarePaginator( $this->forPage($page, $perPage), $this->count(), $perPage, $page, $options)) ->withPath(''); }); }
Go to the test and found page is back.