Hello Artisan,
In this Laravel 9 pagination example, I will discuss step by step how we can create pagination in Laravel. If you don't know how to create pagination, then this laravel 9 pagination example is for you.
I am here to guide you on how to create pagination in laravel. I will explain simply about laravel 9 pagination bootstrap 5 examples. This tutorial will give you a complete example of the laravel 9 pagination with tailwind example. You will learn how to add pagination in laravel 9 step by step.
You know that Laravel has an inbuilt pagination method paginate() to create pagination and we will use that to create our pagination in the Laravel 9 application. So you need to just follow a few steps.
Step 1: Install Laravel 9
In this first step, we are going to download a fresh Laravel 9 application. So download it like:
composer create-project laravel/laravel example-app
Step 2: Configure Database
In the second step, we will make database configuration for the example database name, username, password, etc for our crud application of laravel 9. So let's open the .env
file and fill in all details like as below:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name(blog)
DB_USERNAME=here database username(root)
DB_PASSWORD=here database password(root)
Step 3: Create Dummy Users
In this step, we need to run the migration command to create a users table and then create dummy users records so we can see pagination. So run php artisan migrate
and then run the below command:
php artisan tinker
User::factory()->count(100)->create()
Step 4: Create Route
The first thing is we put one route in one for list users with pagination. So simply add both routes to your route file.
routes/web.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 5: Create Controller
Same things as above for route, here we will add one new method for the route. index() will return users with pagination data, so let's add bellow:
app/Http/Controllers/UserController.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(Request $request)
{
$users = User::paginate(5);
return view('users', compact('users'));
}
}
Step 6: Create Blade File
In this step, you need to create the user's blade file and put below code with links() so it will generate pagination automatically. So let's put it.
resources/views/users.blade.php
Read also: Create Custom Pagination with Pretty URL in Laravel
Hope this Laravel 9 pagination example will help you.
#laravel #laravel-9x