Laravel & Eloquent: Pagination Examples

Updated: January 16, 2024 By: Guest Contributor Post a comment

Introduction

Dealing with datasets in web applications can be tricky – especially when it comes to handling large amounts of data while ensuring the application remains responsive. In this guide, we will demonstrate the use of Laravel’s Eloquent pagination, diving into a spectrum of pagination examples designed to optimize your web applications, ensuring they’re both functionally rich and user-friendly.

Understanding the Basics of Pagination with Laravel & Eloquent

Eloquent ORM makes tasks such as pagination simple and seamless. Laravel’s paginator is integrated with the query builder and Eloquent ORM and provides convenient, easy-to-use pagination of database results out of the box.

Pagination is the process of dividing a dataset into discrete pages, effectively limiting the number of data entries presented at any one time. This helps to conserve resources and improve user experience.

Getting Started with Basic Pagination

The simplest form of pagination can be achieved by using the paginate method provided by the Eloquent ORM. Here is a basic example:


// Using the paginate method on a Query Builder instance
$users = DB::table('users')->paginate(15);

The above code will provide us with a simple paginated list of users, 15 per page.

Controller Example:


public function showUsers()
{
    $users = User::paginate(15);
    return view('users.index', compact('users'));
}

This code fetches the users from the database and passes them to the view with pagination applied.

Anatomy of the Pagination View

Once we have paginated items, we can display them using Laravel’s built-in pagination views. Below is an example of how this can be implemented within a Blade template:


@foreach ($users as $user)
    // Display user data
@endforeach

// Pagination links
{{ $users->links() }}

The links() method will generate the necessary HTML to display pagination links.

Customizing the Pagination View

Laravel provides a simple way to customize the views used to render pagination links. By default, there are two views provided; ‘default’ and ‘simple-bootstrap-4’.


// Inside a Blade template
{{ $users->links('view.name') }}

To customize it, publish the vendor templates and modify them according to your needs:


php artisan vendor:publish --tag=laravel-pagination

Advanced Eloquent Pagination Scenarios

Sometimes, you might need more control over the pagination process. Below are some advanced examples:

Customizing The Paginator Instance


use Illuminate\Pagination\Paginator;

Paginator::currentPageResolver(function () use ($currentPage) {
    return $currentPage;
});

This allows you to manually define the current page.

Conditionally Appending Query Strings


$users = User::paginate(15)->appends(['sort' => 'votes']);

This appends a query string to the pagination links, preserving other query strings on navigation.

JSON Pagination

Laravel also supports pagination for API development. Eloquent makes it particularly easy to convert your paginated results to JSON:


Route::get('/users', function () {
    return User::paginate();
});

This implicit conversion allows client-side frameworks to handle pagination easily.

Conclusion

Through this guide, we have explored the key aspects of implementing pagination in Laravel applications using Eloquent. Whether through rapid implementation of basic pagination or more specific customizations, Laravel’s integrated pagination functionality allows for clean and efficient data handling and user experience enhancements within your web applications.