Laravel Pagination Guide
November 16, 2024 ยท AI Conversations
### 1. **Basic Pagination**
To paginate data, you can use the `paginate()` method on an Eloquent query or a query builder instance.
Example for paginating posts:
$posts = Post::paginate(10);
This will retrieve 10 posts per page. You can pass the number of items per page as an argument to `paginate()`.
In your view, you can display the pagination links like this:
@foreach ($posts as $post)
<p>{{ $post->title }}</p>
@endforeach
<!-- Display pagination links -->
{{ $posts->links() }}
### 2. **Customizing the Pagination View**
You can customize the pagination view by publishing the pagination views:
php artisan vendor:publish --tag=laravel-pagination
After publishing, you can modify the `resources/views/vendor/pagination` files for custom pagination layouts.
### 3. **Advanced Pagination**
If you want to paginate results based on a query with relationships or more complex data:
$posts = Post::with('comments')->paginate(10);
### 4. **Using Query Parameters with Pagination**
You can customize the pagination links by preserving query parameters when paginating.
$posts = Post::paginate(10)->appends(request()->query());
This ensures the current query parameters are preserved when navigating through pages.
### 5. **JSON Pagination**
If you're using pagination in an API or returning JSON data, you can use the `json` method for paginated results:
return response()->json($posts);
The result will automatically include pagination metadata like `current_page`, `last_page`, etc.
### 6. **Simple Pagination**
If you only need "Previous" and "Next" links (without displaying page numbers), use `simplePaginate()`:
$posts = Post::simplePaginate(10);
This will reduce the complexity of the pagination UI by only showing the previous and next links.
—
Let me know if you need help with any specific pagination use case!
### Steps to Use Bootstrap Pagination in Laravel:
#### 1. **Ensure Bootstrap is Loaded**
Make sure you have Bootstrap included in your project. If you're using a CDN, add the following to your `resources/views/layouts/app.blade.php` (or whichever layout file you're using):
<!-- Add Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
If you're using Laravel Mix with your own asset pipeline, ensure Bootstrap is included in your assets.
#### 2. **Basic Pagination in Controller**
In your controller, retrieve the paginated data using the `paginate()` method as you normally would:
$posts = Post::paginate(10);
#### 3. **Display Pagination in Blade View**
In your Blade view, you can display the pagination links using the `links()` method. By default, Laravel's `links()` method generates pagination links with Bootstrap classes if Bootstrap is detected.
@foreach ($posts as $post)
<div class="post">
<h2>{{ $post->title }}</h2>
<p>{{ $post->content }}</p>
</div>
@endforeach
<!-- Display pagination links with Bootstrap styling -->
<div class="pagination justify-content-center">
{{ $posts->links() }}
</div>
The `links()` method will automatically generate Bootstrap-compatible pagination links like this:
<ul class="pagination justify-content-center">
<li class="page-item"><a class="page-link" href="?page=1">1</a></li>
<li class="page-item"><a class="page-link" href="?page=2">2</a></li>
<li class="page-item"><a class="page-link" href="?page=3">3</a></li>
<!-- Previous and Next buttons -->
<li class="page-item disabled"><span class="page-link">Previous</span></li>
<li class="page-item"><a class="page-link" href="?page=2">Next</a></li>
</ul>
#### 4. **Customizing Pagination Views**
If you want to further customize the pagination appearance or behavior, you can publish the pagination views and modify them.
Run the following Artisan command to publish the pagination views:
php artisan vendor:publish --tag=laravel-pagination
This will publish the pagination view to `resources/views/vendor/pagination`. You can modify the `bootstrap-4.blade.php` (or `bootstrap-5.blade.php` depending on the version you're using) to customize the layout.
#### Example for Customizing Pagination View:
If you're using Bootstrap 5, the published view `resources/views/vendor/pagination/bootstrap-5.blade.php` can be modified to change the appearance of the pagination links, for example:
<ul class="pagination justify-content-center">
@if ($paginator->onFirstPage())
<li class="page-item disabled"><span class="page-link">« Previous</span></li>
@else
<li class="page-item"><a class="page-link" href="{{ $paginator->previousPageUrl() }}">« Previous</a></li>
@endif
@foreach ($elements as $element)
@if (is_string($element))
<li class="page-item disabled"><span class="page-link">{{ $element }}</span></li>
@elseif (is_array($element))
@foreach ($element as $page => $url)
@if ($page == $paginator->currentPage())
<li class="page-item active" aria-current="page"><span class="page-link">{{ $page }}</span></li>
@else
<li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li>
@endif
@endforeach
@endif
@endforeach
@if ($paginator->hasMorePages())
<li class="page-item"><a class="page-link" href="{{ $paginator->nextPageUrl() }}">Next »</a></li>
@else
<li class="page-item disabled"><span class="page-link">Next »</span></li>
@endif
</ul>
—
With this setup, you should have Bootstrap-styled pagination in your Laravel project, automatically using Bootstrap classes to style the pagination links. Let me know if you need further customization!