How to use Pagination in Laravel 11
In this article we will learn about pagination, what and how to use pagination in Laravel 11 with proper example. As we know Laravel provide many features and functionalities to deal with complex requirements efficiently. One of them is pagination. If we want to do pagination manually on both Front-End and Back-End, then its quite a headache. Laravel makes it easy to implement in no time. So let's see how it works.
What is Pagination :
Pagination is basically a process to break-down large data sets and make them into smaller and manageable data sets to work with. It helps processing small dataset in the form of multiple pages on a web page to reduce load time and efficient data interaction.
Benefits :
- Performance Improvement
- Reduced data loading time
- Manageable and user centric data visibility
- Reduce load on database
- Enhanced user experience
Create a Route :
Route::get('/users', [UserController::class, 'getUsers']);
UserController.php :
<?php namespace App\Http\Controllers; use App\Models\User; use Illuminate\Http\Request; class UserController extends Controller { public function getUsers(){ $users = User::paginate(10); return view('users',compact('users')); } }
users.blade.php :
<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link rel="preconnect" href="https://fonts.bunny.net"> <link href="https://fonts.bunny.net/css?family=figtree:400,600&display=swap" rel="stylesheet" /> <!-- Styles --> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> @if (!empty($users)) <h1 class="my-5">Laravel 11 pagination by thedevnerd</h1> <table class="table table-dark"> <thead> <tr> <th scope="col">#</th> <th scope="col">Name</th> <th scope="col">Email</th> <th scope="col">Created At</th> <th scope="col">Updated At</th> </tr> </thead> <tbody> @foreach ($users as $user) <tr> <th scope="row">{{$user->id}}</th> <td>{{$user->name}}</td> <td>{{$user->email}}</td> <td>{{$user->created_at}}</td> <td>{{$user->updated_at}}</td> </tr> @endforeach </tbody> </table> {{ $users->links() }} @else <div class="no-users"> <h3>No users found.</h3> </div> @endif </div> </body> </html>
Possible Error you might face :
How to fix the error :
AppServiceProvider.php:
- Paginator::useBootstrapFive()
- Paginator::useBootstrapFour()
<?php namespace App\Providers; use Illuminate\Pagination\Paginator; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Register any application services. */ public function register(): void { // } /** * Bootstrap any application services. */ public function boot(): void { Paginator::useBootstrapFive(); Paginator::useBootstrapFour(); } }
Advanced Implementation on Pagination :
php artisan vendor:publish --tag=laravel-pagination
Apart from these you can also refer the full documentation regarding Laravel pagination from the official site.