Keywords: Laravel 5 | Custom Pagination | View Templates | PHP Development | Web Framework
Abstract: This article provides a comprehensive technical analysis of implementing custom pagination views in Laravel 5 framework. It examines the evolution from Laravel 4.2 to 5.0 pagination mechanisms, with detailed focus on using @include directive with custom view files. Complete code examples and implementation steps are provided, along with comparisons of different Laravel 5 sub-version implementations, offering practical technical guidance for developers.
Changes in Laravel 5 Pagination Mechanism
In Laravel 4.2, developers could specify custom pagination views through the 'pagination' option in the app/config/view.php configuration file, for example:
'pagination' => 'pagination_slider-alt'
However, in Laravel 5, this configuration approach has been removed, requiring developers to adopt new methods for implementing custom pagination functionality.
Core Implementation Method
In Laravel 5, pagination functionality can be implemented using the @include directive combined with custom view files. The specific implementation steps are as follows:
Basic Syntax Structure
In the main view file, use the following syntax to include custom pagination views:
@include('view.name', ['object' => $users])
Where view.name is the file path of the custom pagination view, and $users is the object containing pagination data.
Custom Pagination View Implementation
Create a custom pagination view file, for example resources/views/pagination/custom.blade.php:
@if ($object->lastPage() > 1)
<ul class="pagination">
<li class="{{ ($object->currentPage() == 1) ? ' disabled' : '' }}">
<a href="{{ $object->url(1) }}">Previous</a>
</li>
@for ($i = 1; $i <= $object->lastPage(); $i++)
<li class="{{ ($object->currentPage() == $i) ? ' active' : '' }}">
<a href="{{ $object->url($i) }}">{{ $i }}</a>
</li>
@endfor
<li class="{{ ($object->currentPage() == $object->lastPage()) ? ' disabled' : '' }}">
<a href="{{ $object->url($object->currentPage()+1) }}" >Next</a>
</li>
</ul>
@endif
Available Pagination Methods
In custom pagination views, the following pagination methods can be accessed through the $object variable:
currentPage()- Get current page numberlastPage()- Get total number of pagesperPage()- Get number of items per pageurl($page)- Generate URL for specified page numbertotal()- Get total number of records
Implementation Differences Across Laravel 5 Versions
Laravel 5.0 - 5.2 Versions
In these versions, the @include approach is recommended:
@include('pagination.default', ['paginator' => $users])
Laravel 5.3+ Versions
Starting from Laravel 5.3, a more concise links() method is provided:
{{ $users->links('pagination.default') }}
Advanced Pagination Features
Limiting Displayed Link Count
For pagination with large numbers of pages, limiting the number of displayed links can improve user experience:
<?php
$link_limit = 7;
?>
@if ($object->lastPage() > 1)
<ul class="pagination">
<li class="{{ ($object->currentPage() == 1) ? ' disabled' : '' }}">
<a href="{{ $object->url(1) }}">First</a>
</li>
@for ($i = 1; $i <= $object->lastPage(); $i++)
<?php
$half_total_links = floor($link_limit / 2);
$from = $object->currentPage() - $half_total_links;
$to = $object->currentPage() + $half_total_links;
if ($object->currentPage() < $half_total_links) {
$to += $half_total_links - $object->currentPage();
}
if ($object->lastPage() - $object->currentPage() < $half_total_links) {
$from -= $half_total_links - ($object->lastPage() - $object->currentPage()) - 1;
}
?>
@if ($from < $i && $i < $to)
<li class="{{ ($object->currentPage() == $i) ? ' active' : '' }}">
<a href="{{ $object->url($i) }}">{{ $i }}</a>
</li>
@endif
@endfor
<li class="{{ ($object->currentPage() == $object->lastPage()) ? ' disabled' : '' }}">
<a href="{{ $object->url($object->lastPage()) }}">Last</a>
</li>
</ul>
@endif
Best Practice Recommendations
In actual development, it's recommended to encapsulate pagination logic in separate view files for easier maintenance and reusability. Additionally, choose the appropriate pagination implementation method based on your Laravel version to ensure code compatibility and maintainability.