How To Use Built-In Laravel Pagination Feature On Website

Are you looking to integrate built-in Laravel pagination in your application? Pagination is always a lengthy task. If we are doing it without any third-party library then we need to handle paginate links, total records, records per page, etc. In this article, we show you how to use pagination in Laravel which is already bundled with Laravel installation.

On a website, we frequently need paginations. To show the large set of records we use pagination. By using pagination, we split a large set of data into small parts. It will reduce the load on our server and as a result, the server responds fast.

How To Use Laravel Pagination

Let’s say we have table ‘products’ in our database and this table contains a large set of records. Our goal is to show 10 rows at a time from this table. And for the next records, we will use the paginated links.

Laravel Pagination

In our controller, we use the paginate method and pass the value 10 as the argument.

public function index()
{
    $products = DB::table('products')->paginate(10);
    return view('products.index', ['products' => $products]);
}

Above code will fetch 10 rows from the database table and pass those records to the view.

For displaying these records in the view, we write the code as follows.

<table class="table">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        @foreach($products as $p)
            <tr>
                <td>{{ $p->id }}</td>
                <td>{{ $p->name }}</td>
                <td>{{ $p->description }}</td>
            </tr>
        @endforeach
    </tbody>
</table>
 
{{ $products->links() }}

Here links method returns a view of pagination which is based on bootstrap table pagination.

Laravel also allows us to use paginate Eloquent queries instead of the query builder.

$products = App\Product::paginate(10);

Pass Extra Parameters To Paginate Links

Normally we apply pagination with the HTML tables. Along with the table list of records, we give the search field to filter records. To persist search filters, we should pass the search string in paginate links like below.

http://YOUR_SITE_DOMAIN/products?s=test&page=1

To achieve this we need to pass a search string to view from the controller.

$products = DB::table('products')->paginate(10);
$products->search = 'YOUR_SEARCH_STRING';
return view('products.index', ['products' => $products]);

Next, to display paginate links we modify our code in view as follows.

{{ $products->appends(['s' => $products->search])->links() }}

appends method add a query string to the pagination links.

We hope you understand how to Laravel pagination in your application. If you have any questions or suggestions please leave a comment below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

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