Laravel

Pagination in Laravel

March 3, 2018

author:

Pagination in Laravel

Pagination is a headache in many other frameworks. But Laravel eliminates this painful experience and can intelligently decide the range on the basis of current page. It generates HTML which is also compatible with Bootstrap. There are many ways to achieve this, which we will see here:

# Pagination with Query Builder

This is probably one of the simplest ways for pagination. Laravel provides paginate() method which automatically sets limits and offsets on the basis of active page viewed by the user. By default, active page is detected with the value of ?page query string on the HTTP request. Laravel performs this in the back end and inserts the generated links on the page.

# app/Http/Controllers/PostController.php

public function index()
{
    //pagination - Query Builder
    $posts = DB::table('posts')->paginate(12);
    return view('post.index', ['posts' => $posts]);
}

The only argument to be passed to paginate() method is the total number of items to be display per page. In the above example, we passed 12 as an argument.

Also, in case if we don’t need a fully functional pagination, we can also have a simpler version of it with only Next and Previous links. simplePaginate() method can perform this efficiently.

# app/Http/Controllers/PostController.php

public function index()
{
    //simple pagination
    $posts = DB::table('posts')->simplePaginate(12);
    return view('post.index', ['posts' => $posts]);
}

# Pagination with Eloquent ORM

We can use the same paginate() method with Eloquent also. It accepts the same argument and performs in the same way as with Query Builder. But it makes the code cleaner and more readable. Here’s how:

# app/Http/Controllers/PostController.php

public function index()
{
    //eloquent
    $posts = Post::paginate(12);
    return view('post.index', ['posts' => $posts]);
}

As with Query Builder, we can use simplePaginate() method with Eloquent also:

# app/Http/Controllers/PostController.php

public function index()
{
    //simple pagination (eloquent)
    $posts = Post::where('category', 'Laravel')->simplePaginate(12);
    return view('post.index', ['posts' => $posts]);
}

# Displaying Pagination Results

Once the pagination data is retrieved , it is very easy to display to the users with layout views. Both paginate() and simplePaginate() methods return paginator instance. They both have several methods to work with pagination. To render data on the screen, we can use loops and iterate through the results using Laravel’s template engine:

<div class="container">
    @foreach ($posts as $post)
        {{ $posts->title }}
    @endforeach
</div>

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

Note that the render method in the end is used to display page links for rest of the results. We don’t need to bind page numbers to it, Laravel already does that with ?page.

Conclusion:

In this tutorial, we covered one of the well-known features of Laravel – The Pagination. We discovered various methods in which Pagination can be done in Laravel Application starting from Query Builder and then moving towards an easy way using Eloquent ORM. We also saw how to render pagination results in Laravel Blade for users.

Questions & Comments:

Thanks for reading. Hope you found this tutorial on Pagination in Laravel useful. If you get stuck at any stage, you can always share your concerns in the comment section below.

Leave a comment

Your email address will not be published. Required fields are marked *