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

<?php

use Illuminate\Support\Facades\Route;

Route::get(‘/’, function () {

return view(‘welcome’);

});

Route::get(‘/users’, ‘UserController@users’);

Let’s see Laravel Pagination with Customization

So, we create UserController.php in Http/Controller folder

<?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’));

}

}

However you should create the Users blade into your resource/Views folder

<!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>

How to create Laravel Pagination with customization?

After that we create pagination blade in the views folder

@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=”&lsaquo; Skip 5″> &lsaquo; 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”>&lsaquo;</span> </li>

@else

<li class=”page-item”><a class=”page-link” href=”{{ $paginator->previousPageUrl() }}” rel=”prev” aria-label=”@lang(‘pagination.previous’)”>&lsaquo;</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’)”>&rsaquo;</a></li>

@else

<li class=”page-item disabled” aria-disabled=”true” aria-label=”@lang(‘pagination.next’)”><span class=”page-link” aria-hidden=”true”>&rsaquo;</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 &rsaquo;”>Skip 5 &rsaquo;</a></li>

@endif

</ul>

</nav>

@endif

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.

$results->count()

On the other hand, get the current page number use below code.

$results->currentPage()
Get your result number of the first item in the results.
$results->firstItem()

Let’s see Get the paginator options.

$results->getOptions()

And create a range of pagination URLs.

$results->getUrlRange($start, $end)

Condition if there are enough items to split into multiple pages.

$results->hasPages()

Condition if there is more items in the data store.

$results->hasMorePages()

For instance, get the items for the current page.

$results->items()

Code for get the result number of the end item

$results->lastItem()
$results->lastPage()

For instance get the URL for the next page.

$results->nextPageUrl()

Condition if the paginator is on the first page.

$results->onFirstPage()

However the number of items to be shown per page.

$results->perPage()

In general, If you need get the URL for the previous page.

$results->previousPageUrl()
Laravel Pagination with Customization

Obviously, You can purchase your hosting from Cloudsurph.comCloudsurph 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).
$results->total()

Therefore, get the URL for a given page number you need to use.

$results->url($page)

However need to get the query string variable used to store the page.

$results->getPageName()

Hence set the query string variable used to store the page.

$results->setPageName($name)

Also, we can see final output for the Laravel Pagination Response given below

{

“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

}

]

}

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.