Laravel 8 custom pagination example
In this tutorial, we will learn Laravel 8 custom pagination examples. In this post how to building and apply a new custom paginator view in Laravel 8. Laravel default provides pagination But our client requirement theme-related pagination. So in this tutorial, we will create our own custom pagination.
Here, we will create some dummy students data and then using those students and show our custom pagination page. we will use the Laravel 8 custom pagination template to do this custom pagination.
- Create Pagination Template
- Define Route
- Create Controller
- Create Blade File
Also read: Laravel 8 CRUD Operation Tutorial for Beginners
Create Pagination Template
First, we are going to use the Laravel custom pagination template. So run bellow command:
php artisan vendor:publish --tag=laravel-pagination Output: Copied Directory [\vendor\laravel\framework\src\Illuminate\Pagination\resources\views] To [\resources\views\vendor\pagination] Publishing complete.
After running the above command, it will get new folder pagination on views files(resources/views/vendor/). And pagination folder by default following copy in \vendor\laravel\framework\src\Illuminate\Pagination\resources\views
the folder:
resources\views\vendor\pagination\
bootstrap-4.blade.php
default.blade.php
semantic-ui.blade.php
simple-bootstrap-4.blade.php
simple-default.blade.php
simple-tailwind.blade.php
tailwind.blade.php
But, We don’t use any of the above templates. in this post, we create a custom pagination template.
Define Route
We need routes to see the custom pagination page. To define the below route and create some dummy data of students tables.
#routes/web.php Route::get('/', 'HomeController@index');
Create Controller
We have to add a new controller method index() in your HomeController. so if you haven’t created a HomeController controller then you can create a controller and paste this below code.
<?php #app/Http/Controllers/HomeController.php namespace App\Http\Controllers; use App\Student; use Illuminate\Http\Request; class StudentController extends Controller { public function index() { $students = Student::latest()->paginate(5); return view('students',compact('students')); } }
Create Blade File
Now we need to create a blade file for students views and a custom pagination template. And create a custom.blade.php file following the path.
<!-- resources/views/vendor/pagination/custom.blade.php --> @if ($paginator->hasPages()) <nav aria-label="Page navigation example"> <ul class="pagination justify-content-center"> @if ($paginator->onFirstPage()) <li class="page-item disabled"> <a class="page-link" href="#" tabindex="-1">Previous</a> </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">{{ $element }}</li> @endif @if (is_array($element)) @foreach ($element as $page => $url) @if ($page == $paginator->currentPage()) <li class="page-item active"> <a class="page-link">{{ $page }}</a> </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() }}" rel="next">Next</a> </li> @else <li class="page-item disabled"> <a class="page-link" href="#">Next</a> </li> @endif </ul> @endif
Now, simply add the custom.blade.php file in the students.blade.php file.
<!-- resources/views/students.blade.php --> @extends('students.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left text-center"> <h2>Laravel 8 CRUD Operation Tutorial for Beginners</h2> </div> <div class="pull-right"> <a class="btn btn-success" href="{{ route('students.create') }}"> Create Student</a> </div><br> </div> </div> @if ($message = Session::get('success')) <div class="alert alert-success"> <span>{{ $message }}</span> </div> @endif <table class="table table-bordered"> <tr> <th>No</th> <th>Firstname</th> <th>Lastname</th> <th>Age</th> <th>Action</th> </tr> @foreach ($students as $student) <tr> <td>{{ ++$i }}</td> <td>{{ $student->firstname }}</td> <td>{{ $student->lastname }}</td> <td>{{ $student->age }}</td> <td> <form action="{{ route('students.destroy',$student->id) }}" method="POST"> <a class="btn btn-info" href="{{ route('students.show',$student->id) }}">Show</a> <a class="btn btn-primary" href="{{ route('students.edit',$student->id) }}">Edit</a> @csrf @method('DELETE') <button type="submit" onclick="return confirm('Do you really want to delete student!')" class="btn btn-danger">Delete</button> </form> </td> </tr> @endforeach </table> {{ $students->links('vendor.pagination.custom') }} @endsection