ONE NET WIKI Free and professional IT Wiki

Strong and easy-to-use Pagination function in Laravel

2018-10-12 ONE NET WIKI

pagination.png

Laravel's paginator is integrated with the query builder and Eloquent ORM and provides convenient, easy-to-use pagination of database results. The HTML generated by the paginator is compatible with the Bootstrap CSS framework.


Paginating Query Builder Results

All you need to do in Laravel is to use the paginate method on the query builder.

$users = DB::table('users')->paginate(15);

If you only need to display simple "Next" and "Previous" links in your pagination view, you can use the simplePaginate method.

$users = DB::table('users')->simplePaginate(15);

Paginating Eloquent Results

All you need to do in Laravel is to use the paginate method on an Eloquent query.

$users = User::paginate(15);
$users = User::where('votes', '>', 100)->paginate(15);

If you only need to display simple "Next" and "Previous" links in your pagination view, you can use the simplePaginate method.

$users = User::where('votes', '>', 100)->simplePaginate(15);

Manually Creating A Paginator

If you try to use paginate method on a collection, you will get an error: Method paginate does not exist.

At this time, you should create a Paginator manually by creating either an Illuminate\Pagination\Paginator or Illuminate\Pagination\LengthAwarePaginator instance.

Paginator:

  • does not need to know the total number of items in the result set.

  • does not have methods for retrieving the index of the last page.

  • Equivalent to the simplePaginate method.

LengthAwarePaginator:

  • require a count of the total number of items in the result set.

  • Equivalent to the Paginate method.

Herein, we create an instance of LengthAwarePaginator to paginate a merged collection.

$items1 = Item::whereBetween('price', [100, 500])->get();
$items1 = Item::whereBetween('price', [1000, 5000])->get();
$items = $items2->merge($items1);
// method paginate() doesn't work with collections
// Error: $items = $items->paginate(15);

$items = $this->paginate($items);
// If you want to customize the URI used by the paginator
// $items = $this->paginate($items, 15, null, ['path'=>route('customize.path')]);
// $items = $this->paginate($items, 15, null, ['path'=>url('customize/path')]);

public function paginate($items, $perPage = 15, $page = null, $options = [])
{
    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    $items = $items instanceof Collection ? $items : Collection::make($items);
    return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}


Displaying Pagination Results

You may display the results and render the page links using Blade:

<div class="container">
    @foreach ($users as $user)
        {{ $user->name }}
    @endforeach
</div>

{{ $users->links() }}

1. Customizing the Paginator URI

You also can customize the URI used by the paginator when generating links using the withPath method.

// http://example.com/custom/url?page=N
$users = User::paginate(15);
$users->withPath('custom/url');

2. Appending To Pagination Links

You may append to the query string of pagination links using the appends method.

// http://example.com/custom/url?sort=age&page=N
{{ $users->appends(['sort' => 'age'])->links() }}
// for multiple request parameters
{{ $properties->appends(request()->input())->links() }}

If you wish to append a "hash fragment" to the paginator's URLs, you may use the fragment method.

// http://example.com/custom/url?page=N#foo
{{ $users->fragment('foo')->links() }}

3. Adjusting The Pagination Link Window

You may control how many additional links are displayed on each side of the paginator's URL "window" using the onEachSide method.

// By default, 3 links on each side
{{ $users->onEachSide(5)->links() }}
Editor's Note: All the articles in this website are original. If reproduced, please indicate the source and link.
1360
0
All Comments (0)
Expand to publish a comment

Web Development


Web Management


Web Marketing


Web Security