How To Learn Pagination In Laravel Through A Practical Example Tutorial

Hello Geek, This tutorial will show you a Laravel pagination example blade. I simply explained the laravel pagination bootstrap 5 example. This tutorial will walk you through a simple example of laravel pagination tailwind. You will learn how to add pagination to Laravel in this tutorial. So, let’s go through a few steps to see how to create pagination in Laravel .

We understand that pagination is a critical component of every project. So, if you’re new to laravel, you should know how to use pagination in laravel 9 and what other functions you can use with laravel pagination.

In this example, we will run the migration and create a table called “users.” The tinker command will then be used to create dummy records. The users will then be displayed with pagination. Laravel pagination uses the Tailwind CSS design by default; we will use the Bootstrap 5 design for pagination here.So, let’s go over the following tutorials:

Step 1: Install Laravel

This is optional; however, if you have not yet created the laravel app, you may proceed with the following command:

composer create-project laravel/laravel example-app

Step 2: Database Configuration

In second step, we will make database configuration for example database name, username, password etc for our crud application of laravel . So let’s open .env file and fill all details like as below:

DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_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 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 tinkerUser::factory()->count(100)->create()

Step 4: Add Route

The first thing we did was create a single route in one for list users with pagination. Simply include both routes in your route file.

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: Create Controller

The same as with route above, we will add one new route method here. Index() will return users with pagination data, so add the following:

<?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 users blade file and put bellow code with links() so it will generate pagination automatically. So let’s put it.

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Pagination Example - aboutthenerd.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>Laravel 9 Pagination Example - aboutthenerd.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 the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

Leave a Reply