Laravel 9 Pagination Example Tutorial
In this post we will give you information about Laravel 9 Pagination Example Tutorial with Bootstrap 4 Modal and Pagination. Hear we will give you detail about Laravel 9 Pagination Example Tutorial with Bootstrap 4 Modal and Pagination And how to use it also give you demo for it if it is necessary.
Are you looking for example of Laravel 9 pagination example blade. if you have question about Laravel 9 pagination with user table then i will give simple example with solution. i explained simply step by step Laravel 9 pagination tutorial. let’s discuss about pagination in Laravel 9.
We know pagination is a primary requirement of each and every project. so if you are beginner with laravel than you must know how to use pagination in Laravel 9 and what is other function that can use with Laravel 9 pagination.
In this example i will explain you from scratch how to working with laravel pagination. so let’s follow bellow tutorial for creating simple example of pagination with Laravel 9.
Step 1: Add Route
First thing is we put one route in one for list users with pagination. So simple add both routes in your route file.
routes/web.php
<?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
Same things as above for route, here we will add one new method for route. index() will return users with pagination data, so let’s add bellow:
app/Http/Controllers/UserController.php
<?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, you need to create users blade file and put bellow code with links() so it will generate pagination automatically. So let’s put it.
resources/views/users.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 9 Pagination Example Tutorial with Bootstrap 4 Modal and Pagination</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 9 Pagination Example Tutorial with Bootstrap 4 Modal and Pagination</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>
Now you can run and check this example. it is a very simple and basic example.
If you are using bootstrap then you have to add useBootstrap() on service provider as like bellow:
app\Providers\AppServiceProvider.php
<?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(); } }
If you need advance used of pagination then you can see bellow how to use.
Pagination with appends parameter
{!! $data->appends(['sort' => 'votes'])->links() !!}
Pagination with appends request all parameters
{!! $data->appends(Request::all())->links() !!}
Conclusion for Laravel 9 Pagination Example Tutorial
Finally, we have completed the Laravel 9 Pagination tutorial with an example. In this tutorial, and we have learned how to work with pagination in Laravel. We got to know about setting up databases, creating models and migrations, creating fake data with faker, creating controller, route and blade view file, and most importantly, we learned to use pagination component in laravel.
We understood the various other methods, such as passing custom parameters with pagination and converting pagination results to JSON.
Hope this code and post will helped you for implement Laravel 9 Pagination Example Tutorial with Bootstrap 4 Modal and Pagination. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us.