Pagination

Generate the HTML for paginating your tables

[Since 0.7.2]

Introduction

With Pletfix's paginator, you can simply paginate database tables and other lists.

Paginator - Slider at the left site Paginator - Slider in the middle Paginator - Slider at the right site

Basic Usage

You may use the paginator function to create a new Paginator instance:

$paginator = paginator($total, $limit);

The first argument passed to the paginate method is the total number of items. The second argument specified the maximum of items you would like displayed "per page". You may also set the current page as third argument, but, of course, this value is automatically detected.

Limit the Source

The following example illustrates a typical use case with a Query Builder into a controller action method:

public function index()
{
    $builder   = User::builder();
    $paginator = paginator($builder->count(), 10);
    $users     = $builder
                    ->offset($paginator->offset())
                    ->limit($paginator->limit())
                    ->all();

    return view('users.index', compact('paginator', 'users'));
}

In this example we have specify that we would like to display 10 user accounts per page. The view 'users.index' have to include a partial view to render the paginator control, for example:

@extends('app')

@section('content')
    <h2>Users</h2>

    <div class="table-responsive">
        <!-- show table data -->
    </div>

    @include('_pagination')
@endsection

Render the Paginator

The Pletfix Application Skeleton has already placed the partial view _pagination.blade.php in the view directory to rendering the pagination control. The partial view is compatible with the Bootstrap CSS framework. Feel free to customize this as you wish.

@if ($paginator->hasMultiplePages())
    <ul class="pagination">
        @if ($url = $paginator->previousUrl())
            <li><a href="{{$url}}" title="previous"><span>&laquo;</span></a></li>
        @else
            <li class="disabled"><span>&laquo;</span></li>
        @endif
        @foreach ($paginator->pages() as $page)
            @if($page == '...')
                <li class="disabled"><span>...</span></li>
            @elseif ($page == $paginator->currentPage())
                <li class="active"><span>{{$page}}</span></li>
            @else
                <li><a href="{{$paginator->url($page)}}">{{$page}}</a></li>
            @endif
        @endforeach
        @if ($url = $paginator->nextUrl())
            <li><a href="{{$url}}" title="next"><span>&raquo;</span></a></li>
        @else
            <li class="disabled"><span>&raquo;</span></li>
        @endif
    </ul>
@endif

As shown above, you may include this partial in your view with the @include directive:

@include('_pagination')

Of course, if the paginator is not stored in the $paginator variable, you have to passed this one as the second argument:

@include('_pagination', ['paginator' => $myPaginator])

Available Methods

Method Listing

currentPage()

The currentPage method gets the current page.

$currentPage = $paginator->currentPage();

getFragment()

The getFragment method gets the URL fragment.

$fragment = $paginator->getFragment();

getPageKey()

The getPageKey method gets the query string variable used to store the page.

$key = $paginator->getPageKey();

getParameters()

The getParameters method gets the query parameters.

$params = $paginator->getParameters();    

hasMultiplePages()

The hasMultiplePages method determines if there are enough items to split into multiple pages.

$canPaginate = $paginator->hasMultiplePages();  

lastPage()

The lastPage method gets the last page.

$canPaginate = $paginator->lastPage();  

limit()

The limit method gets the maximal number of items per page.

$limit = $paginator->limit();  

nextUrl()

The nextUrl method gets the URL for the next page.

$canPaginate = $paginator->nextUrl();  

offset()

The offset method gets the offset of items for the current page.

$offset = $paginator->offset();  

pages()

The pages method gets the pages.

$pages = $paginator->pages(); // e.g. [1, 2, '...', 5, 6, 7, 8, 9, 10, 11, '...', 14, 15]

previousUrl()

The previousUrl method gets the URL for the previous page.

$prevUrl = $paginator->previousUrl();  

setFragment()

The setFragment method sets a URL fragment to add to all URLs.

$paginator->setFragment('foo');  

setPageKey()

The setPageKey method sets the query string variable used to store the page.

$paginator->setPageKey('page');  

setParameters()

The setParameters method sets a query parameters to add to all URLs.

$paginator->setParameters(['foo' => 'bar']);  

total()

The total method gets the total number of items.

$total = $paginator->total();  

url()

The url method gets the URL for a given page number.

┬žurl = $paginator->url(3);  

(edit on GitHub)