In this tutorial you will learn about the Laravel 8 Pagination Example Tutorial and its application with practical example.
In this Laravel 8 Pagination Example Tutorial I’ll show you how to implement pagination in laravel application. In this tutorial you will learn to create pagination using laravel pagination.We will be using default laravel pagination method to create pagination in laravel. In this step by step tutorial I will demonstrate to implement laravel pagination app.
Laravel 8 Pagination Example Tutorial
Step 1: Add Route
First of all we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\UserController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('users', [UserController::class, 'index']); |
Step 2: Create Controller
Now, lets create a controller named UserController using command given below –
1 |
php artisan make:controller <span class="heads">UserController</span> |
Once the above command executed, it will create a controller file UserController.php in app/Http/Controllers/ directory. Open the UserController.php file and put the following code in it.
app/Http/Controllers/UserController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $data = User::paginate(5); return view('users',compact('data')); } } |
Step 3: Create Blade File
In this step, we will create view/blade file. Lets create a blade file “users.blade.php” in “resources/views/” directory and put the following code in it respectively.
resources/views/users.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<!DOCTYPE html> <html> <head> <title>Laravel 8 CRUD Application</title> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet"> </head> <body> <div class="container"> <h1>Laravel 8 Pagination Example</h1> <table class="table table-bordered"> <thead> <tr> <th>Name</th> <th width="300px;">Action</th> </tr> </thead> <tbody> @if(!empty($data) && $data->count()) @foreach($data as $key => $value) <tr> <td>{{ $value->name }}</td> <td> <button class="btn btn-danger">Delete</button> </td> </tr> @endforeach @else <tr> <td colspan="10">There are no data.</td> </tr> @endif </tbody> </table> {!! $data->links() !!} </div> </body> </html> |
In bootstrap you have to add useBootstrap() on service provider as like bellow:
app\Providers\AppServiceProvider.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Pagination\Paginator; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { } /** * Bootstrap any application services. * * @return void */ public function boot() { Paginator::useBootstrap(); } } |
For advance pagination then you can see bellow Pagination with appends parameter
1 |
{!! $data->appends(['sort' => 'votes'])->links() !!} |
Pagination with appends request.
1 |
{!! $data->appends(Request::all())->links() !!} |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/users |