Laravel Pagination with Customization
Laravel Pagination with Customization
How to create Laravel Pagination with customization? How to customize Laravel pagination, if you want or need to customize your Laravel pagination view.
In addition for developing Pagination in Laravel, you have to need to know the basics structure of the MVC (Model-View-Controller) Pattern.
IF you want then buy a good, reliable, secure WordPress web hosting service from here: click here
Now, create your Route for pagination view in web.php
[box type="shadow"]
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/users', 'UserController@users');
[/box]
Let's see Laravel Pagination with Customization
So, we create UserController.php in Http/Controller folder
[box type="shadow"]
<?php
namespace App\Http\Controllers;
class UserController extends Controller
{
/**
- Show the users with paginate.
- @param object $request
- @return View
*/
public function users(Request $request)
{
$users = User::paginate(10);
return view('users', compact('users'));
}
}
[/box]
However you should create the Users blade into your resource/Views folder
[box type="shadow"]
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<table class="table table-bordered table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Avatar</th>
<th scope="col">Email</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Created On</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<th scope="row">{{ $user->id }}</th>
<td> <img src="{{ $user->avatar }}" width="25" height="25" /></td>
<td>{{ $user->email }}</td>
<td>{{ $user->first_name }}</td>
<td>{{ $user->last_name }}</td>
<td>{{ date('d M, Y', strtotime($user->created_at)) }}</td>
</tr>
@endforeach
</tbody>
</table>
<div>
<!-- For Default pagination user -->
<div>{{ $users->links() }}</div>
<!-- For Custom pagination User -->
<div>{{ $users->links('pagination') }}</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
[/box]
How to create Laravel Pagination with customization?
After that we create pagination blade in the views folder
[box]
@if($paginator->hasPages())
<nav>
<ul class="pagination pull-right">
{{-- Previous Page Link --}}
@if($paginator->currentPage() > 5)
<li class="page-item">
<a class="page-link" href="<?php echo $paginator->url( $paginator->currentPage() - 5 ); ?>" rel="prev" aria-label="‹ Skip 5"> ‹ Skip 5 </a>
</li>
@endif
@if ($paginator->onFirstPage())
<li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')">
<span class="page-link" aria-hidden="true">‹</span>
</li>
@else
<li class="page-item"><a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">‹</a></li>
@endif
{{-- Pagination Elements --}}
@foreach ($elements as $element)
{{-- "Three Dots" Separator --}}
@if (is_string($element))
<li class="page-item disabled" aria-disabled="true"><span class="page-link">{{ $element }}</span></li>
@endif
{{-- Array Of Links --}}
@if (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
{{-- Next Page Link --}}
@if ($paginator->hasMorePages())
<li class="page-item"><a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">›</a></li>
@else
<li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')"><span class="page-link" aria-hidden="true">›</span></li>
@endif
@if($paginator->lastPage() >= $paginator->currentPage()+5)
<li class="page-item"><a class="page-link" href="{{ $paginator->url( $paginator->currentPage() + 5 ) }}" rel="prev" aria-label="Skip 5 ›">Skip 5 ›</a></li>
@endif
</ul>
</nav>
@endif
[/box]
Laravel Pagination with Customization
Laravel Pagination with Customization, Below we try to give you some useful pagination function code that you use in Laravel
The number of items get for the current page use below code.
[box type="shadow"]$results->count()[/box]
On the other hand, get the current page number use below code.
[box type="shadow"]$results->currentPage()[/box]
Get your result number of the first item in the results.
[box type="shadow"]$results->firstItem()[/box]
Let's see Get the paginator options.
[box type="shadow"]$results->getOptions()[/box]
And create a range of pagination URLs.
[box type="shadow"]$results->getUrlRange($start, $end)[/box]
Condition if there are enough items to split into multiple pages.
[box type="shadow"]$results->hasPages()[/box]
Condition if there is more items in the data store.
[box type="shadow"]$results->hasMorePages()[/box]
For instance, get the items for the current page.
[box type="shadow"]$results->items()[/box]
Code for get the result number of the end item
[box type="shadow"]$results->lastItem()[/box]
[box type="shadow"]$results->lastPage()[/box]
For instance get the URL for the next page.
[box type="shadow"]$results->nextPageUrl()[/box]
Condition if the paginator is on the first page.
[box type="shadow"]$results->onFirstPage()[/box]
However the number of items to be shown per page.
[box type="shadow"]$results->perPage()[/box]
In general, If you need get the URL for the previous page.
[box type="shadow"]$results->previousPageUrl()[/box]
Laravel Pagination with Customization
Obviously, You can purchase your hosting from Cloudsurph.com, Cloudsurph hosting is more reliable
and comfortable for your business website and it is most secure.
In fact, Check the Condition the total number of matching items in the data store. (It is not available if you are using simplePaginate).
[box type="shadow"]$results->total()[/box]
Therefore, get the URL for a given page number you need to use.
[box type="shadow"]$results->url($page)[/box]
However need to get the query string variable used to store the page.
[box type="shadow"]$results->getPageName()[/box]
Hence set the query string variable used to store the page.
[box type="shadow"]$results->setPageName($name)[/box]
Also, we can see final output for the Laravel Pagination Response given below

[box type="shadow"]
{
"total": 50,
"per_page": 15,
"current_page": 1,
"last_page": 4,
"first_page_url": "http://laravel.app?page=1",
"last_page_url": "http://laravel.app?page=4",
"next_page_url": "http://laravel.app?page=2",
"prev_page_url": null,
"path": "http://laravel.app",
"from": 1,
"to": 15,
"data":[
{
// Result Object
},
{
// Result Object
}
]
}
[/box]
Customize Laravel Pagination
Finally, if you enjoyed reading this article and have more questions please reach out to our support team via live chat or email and we would be glad to help you. Laravel Pagination with Customization
we provide server hosting for all types of need and we can even get your server up and running with the service of your choice.