Pagination

In other frameworks, pagination can be very painful. Laravel makes it a breeze. Laravel can quickly generate an intelligent "range" of links based on the current page, and the generated HTML is compatible with the .

There are several ways to paginate items. The simplest is by using the method on the query builder or an . The paginate method provided by Laravel automatically takes care of setting the proper limit and offset based on the current page being viewed by the user. By default, the current page is detected by the value of the ?page query string argument on the HTTP request. Of course, this value is automatically detected by Laravel, and is also automatically inserted into links generated by the paginator.

First, let's take a look at calling the paginate method on a query. In this example, the only argument passed to paginate is the number of items you would like displayed "per page". In this case, let's specify that we would like to display 15 items per page:

"Simple Pagination"

If you only need to display simple "Next" and "Previous" links in your pagination view, you have the option of using the simplePaginate method to perform a more efficient query. This is very useful for large datasets if you do not need to display a link for each page number when rendering your view:

    1. $users = App\User::paginate(15);

    Of course, you may call paginate after setting other constraints on the query, such as where clauses:

    You may also use the simplePaginate method when paginating Eloquent models:

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

    Sometimes you may wish to create a pagination instance manually, passing it an array of items. You may do so by creating either an Illuminate\Pagination\Paginator or Illuminate\Pagination\LengthAwarePaginator instance, depending on your needs.

    The Paginator class does not need to know the total number of items in the result set; however, because of this, the class does not have methods for retrieving the index of the last page. The LengthAwarePaginator accepts almost the same arguments as the Paginator; however, it does require a count of the total number of items in the result set.

    In other words, the Paginator corresponds to the simplePaginate method on the query builder and Eloquent, while the LengthAwarePaginator corresponds to the paginate method.

    When manually creating a paginator instance, you should manually "slice" the array of results you pass to the paginator. If you're unsure how to do this, check out the PHP function.

    So, once you have retrieved the results, you may display the results and render the page links using :

    1. <div class="container">
    2. @foreach ($users as $user)
    3. {{ $user->name }}
    4. @endforeach
    5. </div>
    6. {{ $users->links() }}

    The links method will render the links to the rest of the pages in the result set. Each of these links will already contain the proper ?page query string variable. Remember, the HTML generated by the links method is compatible with the Bootstrap CSS framework.

    Customizing The Paginator URI

    The setPath method allows you to customize the URI used by the paginator when generating links. For example, if you want the paginator to generate links like http://example.com/custom/url?page=N, you should pass custom/url to the setPath method:

    You may add to the query string of pagination links using the appends method. For example, to append &sort=votes to each pagination link, you should make the following call to appends:

    1. {{ $users->appends(['sort' => 'votes'])->links() }}

    If you wish to append a "hash fragment" to the paginator's URLs, you may use the fragment method. For example, to append #foo to the end of each pagination link, make the following call to the fragment method:

    1. {{ $users->fragment('foo')->links() }}

    Additional Helper Methods

    You may also access additional pagination information via the following methods on paginator instances:

    • $results->count()
    • $results->currentPage()
    • $results->firstItem()
    • $results->hasMorePages()
    • $results->lastPage() (Not available when using simplePaginate)
    • $results->nextPageUrl()
    • $results->perPage()
    • $results->total() (Not available when using simplePaginate)
    • $results->url($page)

    The Laravel paginator result classes implement the Illuminate\Contracts\Support\JsonableInterface contract and expose the toJson method, so it's very easy to convert your pagination results to JSON.

    The JSON from the paginator will include meta information such as total, current_page, last_page, and more. The actual result objects will be available via the data key in the JSON array. Here is an example of the JSON created by returning a paginator instance from a route:

    Example Paginator JSON

    1. {
    2. "total": 50,
    3. "per_page": 15,
    4. "current_page": 1,
    5. "last_page": 4,
    6. "next_page_url": "http://laravel.app?page=2",
    7. "prev_page_url": null,
    8. "from": 1,
    9. "to": 15,
    10. "data":[
    11. {
    12. // Result Object
    13. },
    14. {
    15. // Result Object
    16. }