How to create Customize pagination templates in Laravel 8?

Today now in this post i will show you How to create Customize pagination templates in Laravel 8? Here I am going to tell you about how we can building and apply the new custom paginator view in Laravel application.

By Default Laravel is provide us the very simple bootstrap pagination in view. But if we are not using bootstrap and we want to change then we can simply change OR if we want to add new bootstrap pagination layout then we can do this.

Now in the bellow example we can see how we can create the custom blade layout and use them. So using this example from the scratch so we can simply implement it in our project or laravel application.

Ok, so at first need to run the bellow command:

php artisan vendor:publish --tag=laravel-pagination

After the bellow command run we will get a new folder as name “pagination” on the views files(resources/views/vendor). Now in the pagination folder we will get the following files by default:

  1. default.blade.php
  2. bootstrap-4.blade.php
  3. simple-bootstrap-4.blade.php
  4. simple-default.blade.php

We can also use the bellow blade template for the pagination and also we can change on this files.

Step 1: Create New Table with Data

At first step, first we have to create new one table also with some records. In this example i will created a new table “posts” and with some dummy data

Step 2: Add New Route

After the created a table with the dummy data, we need to add a new route for this example. so now need to add bellow new “posts” route in web.php file.

routes/web.php

Route::get('posts', 'HomeController@tags');
Step 3: Add Controller Method

Ok, now we need to add a new controller with method “posts()” in our HomeController. So if we haven’t then need to created HomeController by following command put the bellow code:

command for create controller

php artisan make:controller HomeController

app/Http/Controllers/HomeController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;
use App\Http\Requests;
use DB;


class HomeController extends Controller
{


    public function tags()
    {
    	$tags = DB::table('tags')->paginate(7);
    	return view('tags',compact('tags'));
    }


}

Step 4: Create Blade File For Pagination

Ok, now we need to create a new custom.blade.php file for our custom pagination template. So In this file changed the bootstrap class and also need to add new text for next and previous. Then it seems different layout for the pagination layout.

so at first i am going to create a blade file and put the bellow code:

resources/views/vendor/pagination/custom.blade.php

@if ($paginator->hasPages())
    <ul class="pager">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
            <li class="disabled"><span>← Previous</span></li>
        @else
            <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">← Previous</a></li>
        @endif


        {{-- Pagination Elements --}}
        @foreach ($elements as $element)
            {{-- "Three Dots" Separator --}}
            @if (is_string($element))
                <li class="disabled"><span>{{ $element }}</span></li>
            @endif


            {{-- Array Of Links --}}
            @if (is_array($element))
                @foreach ($element as $page => $url)
                    @if ($page == $paginator->currentPage())
                        <li class="active my-active"><span>{{ $page }}</span></li>
                    @else
                        <li><a href="{{ $url }}">{{ $page }}</a></li>
                    @endif
                @endforeach
            @endif
        @endforeach


        {{-- Next Page Link --}}
        @if ($paginator->hasMorePages())
            <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">Next →</a></li>
        @else
            <li class="disabled"><span>Next →</span></li>
        @endif
    </ul>
@endif
Step 5: Create New Blade File

Now we are at last we need to create posts.blade.php file and we can use our custom pagination template. So let’s create a new page and put the bellow code on it.

resources/views/posts.blade.php

<!DOCTYPE html>
<html>
<head>
	<title>Laravel custom pagination template</title>
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">


	<style type="text/css">
		.my-active span{
			background-color: #5cb85c !important;
			color: white !important;
			border-color: #5cb85c !important;
		}
	</style>

	
</head>
<body>


<div class="container">


	<h1 class="text-primary">Laravel Customize pagination templates</h1>


	<table class="table table-bordered">
		<thead>
			<th>Id</th>
			<th>Name</th>
			<th>Creation Date</th>
			<th>Updated Date</th>
		</thead>
		<tbody>
			@foreach($tags as $key => $tag)
				<tr>
					<td>{{ ++$key }}</td>
					<td>{{ $tag->name }}</td>
					<td>{{ $tag->created_at }}</td>
					<td>{{ $tag->updated_at }}</td>
				</tr>
			@endforeach
		</tbody>
	</table>
</div>


{{ $tags->links('vendor.pagination.custom') }}


</body>
</html>

Read Also: How to implement infinite ajax scroll pagination in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook

Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.