Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Pagination with Laravel

Tuesday, November 17th, 2015 by Servage

pagination-tableIn a recent article the concept behind pagination was described. It is a useful technique to overcome information overload and can fortunately be easily implemented into existing projects. Some frameworks actually have pagination right in their core functionality. In this article it will be demonstrated using Laravel.

Controller pagination example

The example below fetches all products from the database with respect to the current pagination settings. The paginate command states that 10 items should be fetched per page, while the page number would come from URL parameters.

class ProductsController extends Controller
     * Show all of the products
     * @return Response
    public function index()
        $products = DB::table('products')->paginate(10);

        return view('products.index', ['products' => $products]);

You can also use “simplePaginate” instead of “paginate” to get paginated results with just “next” and “previous” buttons rather than buttons for each page. This is particularly a benefit when working with large results where showing a link to each page is not feasible.

Pagination in Laravel works with the default database queries and therefore you can use where conditions and other query based functionality as you like. Essentially adding pagination to existing Laravel systems simply means adding the pagination method.

Showing pagination links in views

The products variable you set in the controller contains a render method which renders the pagination links. It is pretty straightforward to use and just requires a single line of code to get the default pagination links to appear on your site.

{!! $users->render() !!}

Customizing the pagination

There are multiple needs you may have when it comes to rendering the pagination links. For example you may want to set a specific URL for the paginated pages, or you may want to preserve some of the GET parameters of the current URL. That is required for instance when also working with sorting, so that if you have already passed two get parameters that tell your system to sort by price ascending, then you want to resubmit these GET parameters with each pagination link. Otherwise the user would loose the current sorting settings every time a link to a new page is clicked. That would be very bad user experience and should be avoided. Always try to fix such small inconsistency issues. It may not seem like a large bug by itself, but many such smaller oddities will create a general bad impression and annoyance.

JSON pagination

It is also possible to integrate pagination with AJAX. In that case you may have pagination links that load new results (pages) via AJAX, and replace the current results with the new ones. The benefits are the usual AJAX benefits, where content can get changed without the page having to render itself all over again. For this purpose the paginator in the controller also supports being converted to JSON and thereby making the paginated results available via an API.

Pagination with Laravel, 4.8 out of 5 based on 5 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.