Laravel 9 Pagination Bootstrap Example

Mar 08, 2022

.

Admin

Laravel 9 Pagination Bootstrap Example

Hi friends,

Today, I am explaining laravel 9 pagination bootstrap example. In this tutorial, I will share how to create simple pagination in the Laravel 9 application. We will learn to construct simple pagination in laravel 9 application from scratch. I will explain to you how to set a custom path with pagination, how to append input parameter with pagination link etc. laravel 9 pagination with user table then I will give a simple example with a solution.

In this example, I will explain to you from scratch how to work with laravel pagination. so let's follow the below tutorial for creating a simple example of pagination with laravel 9.

Step 1: Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Step 2: Database Configuration

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: Add Dummy Users

In this step, we need to run migration command to create users table and then create dummy users records so we can see pagination.

Let's run migration command:

php artisan migrate

Next, run ticker command to add dummy users:

php artisan tinker

User::factory()->count(100)->create()

Step 4: 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 5: Add 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(Request $request)

{

$users = User::paginate(5);

return view('users', compact('users'));

}

}

Step 6: Add 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 Bootstrap Example - Nicesnippets.com</title>

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">

<h1 class="text-center mt-5 mb-5">Laravel 9 Pagination Bootstrap Example - Nicesnippets.com</h1>

<table class="table table-bordered data-table">

<thead>

<tr>

<th>ID</th>

<th>Name</th>

<th>Email</th>

</tr>

</thead>

<tbody>

@forelse($users as $user)

<tr>

<td>{{ $user->id }}</td>

<td>{{ $user->name }}</td>

<td>{{ $user->email }}</td>

</tr>

@empty

<tr>

<td colspan="3">There are no users.</td>

</tr>

@endforelse

</tbody>

</table>

<!--

You can use Tailwind CSS Pagination as like here:

{!! $users->withQueryString()->links() !!}

-->

{!! $users->withQueryString()->links('pagination::bootstrap-5') !!}

</div>

</body>

</html>

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/users

Output:

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() !!}

I hope it can help you....

#Laravel 9