Laravel, a popular PHP web application framework, simplifies the process of creating and managing web applications. One of the most common tasks in web development is pagination, which involves dividing large datasets into smaller, more manageable chunks. Laravel provides a built-in pagination system that makes it easy to paginate query results or collections. This article will walk you through the process of implementing pagination in Laravel applications.
- Getting Started
Before diving into pagination, make sure you have a Laravel application set up and running. If you haven’t already, you can create a new Laravel project using Composer:
composer create-project --prefer-dist laravel/laravel my-pagination-app
Replace “my-pagination-app” with your desired project name.
- Using Eloquent Pagination
Laravel’s Eloquent ORM (Object-Relational Mapping) makes it easy to interact with database tables as if they were objects. The simplest way to paginate query results is to use the paginate() method provided by the Eloquent ORM.
Assuming you have a model named “Post,” you can paginate the results like this:
use App\Models\Post;
// Get the paginated results
$posts = Post::paginate(15);
The paginate() method accepts an optional argument that specifies the number of items to be displayed per page. The default value is 15.
- Displaying Pagination Links
To display pagination links in your Blade templates, simply use the links() method on the paginator instance. For example, in a Blade template, you might have:
<div class="container">
@foreach ($posts as $post)
<div class="post">
<h2>{{ $post->title }}</h2>
<p>{{ $post->content }}</p>
</div>
@endforeach
</div>
<!-- Render the pagination links -->
{{ $posts->links() }}
By default, Laravel will render Bootstrap-styled pagination links. If you prefer a different styling, you can customize the pagination view.
- Customizing the Pagination View
To customize the pagination view, first, publish the default pagination views provided by Laravel:
php artisan vendor:publish --tag=laravel-pagination
This will create a new directory, resources/views/vendor/pagination
, containing the default pagination views. You can now edit these views to suit your needs, or create a new pagination view from scratch.
- Using the Paginator with Query Builder
If you’re not using Eloquent, you can still paginate query results using Laravel’s query builder. First, add the Illuminate\Pagination\Paginator
facade to your controller. Then, use the paginate
method on your query:
use Illuminate\Support\Facades\DB;
use Illuminate\Pagination\Paginator;
// Get the paginated results
$posts = DB::table('posts')->paginate(15);
- Paginating Collections
If you have a collection and want to paginate its items, you can use the paginate
method provided by the Illuminate\Support\Collection
class:
use Illuminate\Pagination\LengthAwarePaginator;
// Paginate the collection
$paginatedCollection = new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, ['path' => LengthAwarePaginator::resolveCurrentPath()]);
- Customizing Pagination URL
By default, Laravel uses the “page” query parameter for pagination. To customize this, you can use the setPageName()
method:
$posts = Post::paginate(15)->setPageName('custom_page');