Laravel

 

Boost Your Laravel Skills: Ultimate Guide to Pagination Techniques

When building web applications, there’s often a need to handle large data sets. Displaying all this data on a single page isn’t practical, both in terms of user experience and system performance. This is where pagination comes into play. Laravel, being a comprehensive and developer-friendly PHP framework, provides out-of-the-box support for pagination.

Boost Your Laravel Skills: Ultimate Guide to Pagination Techniques

In this blog post, we’ll delve deep into Laravel pagination, exploring how to set it up, customize it, and effectively manage large result sets.

1. Basics of Laravel Pagination

Laravel provides an easy way to paginate database results. Let’s consider a basic example using Eloquent, Laravel’s ORM (Object-Relational Mapping).

Suppose you have a `posts` table and you want to display the posts in chunks of 10 per page. Here’s how you can do that:

```php
$posts = \App\Models\Post::paginate(10);
```

In your Blade view, to render the paginated links:

```php
{{ $posts->links() }}
```

2. Customizing the Pagination View

Laravel uses Bootstrap for its default pagination view, but you can easily customize it.

To generate the pagination view, use the following Artisan command:

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

This command will place the views in the `resources/views/vendor/pagination` directory. You can edit these views to customize the pagination appearance as per your needs.

3. Displaying Pagination Information

Laravel allows you to display the current page number, the total number of items, and more. Here’s how you can display this information:

```php
Showing {{ $posts->firstItem() }} to {{ $posts->lastItem() }} of {{ $posts->total() }} posts.
```

4. Using Simple Pagination

When you only need the “Next” and “Previous” links without showing the exact page numbers, Laravel offers simple pagination.

```php
$posts = \App\Models\Post::simplePaginate(10);
```

In your Blade view:

```php
{{ $posts->links() }}
```

5. Paginating Query Builder Results

While the Eloquent model is popular, Laravel also supports paginating results directly from the query builder:

```php
$posts = DB::table('posts')->paginate(10);
```

6. Paginating Results From Collections

Suppose you have a collection and you want to paginate its results. Laravel supports that too:

```php
$collection = collect(range(1, 100));
$paginated = new \Illuminate\Pagination\LengthAwarePaginator(
    $collection->forPage($currentPage, $perPage),
    $collection->count(),
    $perPage,
    $currentPage,
    ['path' => \Illuminate\Pagination\Paginator::resolveCurrentPath()]
);
```

7. Adjusting the Pagination URL

Laravel also provides flexibility to adjust the pagination URL, especially useful for custom routes or when using translated routes.

```php
$posts->withPath('custom/path');
```

8. Appending to Pagination Links

When you have additional query strings that need to be added to pagination links, Laravel makes it easy:

```php
$posts = \App\Models\Post::where('category', 'tech')->paginate(10);
$posts->appends(['category' => 'tech']);
```

The generated links will then have the `category=tech` query string attached.

9. Display Data Conditionally

There might be situations when you only want to display the pagination links if there are more pages to show:

```php
@if ($posts->hasPages())
    {{ $posts->links() }}
@endif
```

10. Customizing Page Name in Query String

By default, Laravel uses the `page` query string to determine the current page. If you wish to use a different name:

```php
$posts = \App\Models\Post::paginate(10, ['*'], 'customName');
```

This will use `customName` as the query parameter for pagination.

Conclusion

Laravel’s pagination is a powerful feature, making it easier to handle large result sets efficiently. Whether you’re dealing with Eloquent models, query builder results, or custom collections, Laravel provides a streamlined way to paginate data. It offers a great blend of functionality and customization, ensuring that developers can provide an optimized user experience without getting bogged down in the intricacies of pagination logic.

Remember, while pagination is a great tool for displaying large data sets, it’s also crucial to consider optimizing your database queries to ensure that your applications run smoothly and efficiently.

Previously at
Flag Argentina
Argentina
time icon
GMT-3
Experienced Full Stack Engineer with expertise in Laravel and AWS. 7 years of hands-on Laravel development, leading impactful projects and teams.