Laravel Pagination Example customizations

Laravel pagination example

Laravel Pagination example with MySQL:

Laravel provides a convenient and powerful pagination feature that allows you to divide large result sets into smaller, more manageable chunks. Laravel pagination is useful when you have a large dataset and want to display it in a paginated manner on your web pages. Here’s how you can use Laravel pagination:

To implement Laravel pagination with a database table and display it in HTML and CSS, you can follow these steps:

Create a database table:

First, create a migration to generate the table in your database. You can use the Laravel Artisan command to generate the migration file:

   php artisan make:migration create_items_table --create=items

This will create a migration file under the database/migrations directory. Open the migration file and define the table structure:


   use Illuminate\Database\Migrations\Migration;
   use Illuminate\Database\Schema\Blueprint;
   use Illuminate\Support\Facades\Schema;

   class CreateItemsTable extends Migration
       public function up()
           Schema::create('items', function (Blueprint $table) {

       public function down()

Run the migration to create the table:

   php artisan migrate

Create a model:

Create a model for the Item table using the Artisan command:

   php artisan make:model Item

In Laravel, you can use the make command along with factories and seeders to insert fake data into your database. Here’s how you can do it:

  1. Create a Factory: First, create a factory that defines the structure of your fake data. Run the following command to generate a factory file:
   php artisan make:factory ItemFactory --model=Item

This command will create an ItemFactory file under the database/factories directory. Open the file and define the factory fields and their respective fake data:


   namespace Database\Factories;

   use App\Models\Item;
   use Illuminate\Database\Eloquent\Factories\Factory;

   class ItemFactory extends Factory
       protected $model = Item::class;

       public function definition()
           return [
               'name' => $this->faker->name,

Here, we’re generating a fake name for each item using the $faker object.

  1. Update the Model: Open the Item model file (app/Models/Item.php) and make sure it uses the HasFactory trait:

   namespace App\Models;

   use Illuminate\Database\Eloquent\Factories\HasFactory;
   use Illuminate\Database\Eloquent\Model;

   class Item extends Model
       use HasFactory;

       // ...

By using the HasFactory trait, you enable the factory functionality for the model.

Create a Seeder:

Now, create a seeder to call the factory and insert the fake data into the database. Run the following command to generate a seeder file:

   php artisan make:seeder ItemSeeder

This command will create an ItemSeeder file under the database/seeders directory. Open the file and update the run() method to insert the fake data:


   namespace Database\Seeders;

   use App\Models\Item;
   use Illuminate\Database\Seeder;

   class ItemSeeder extends Seeder
       public function run()

In this example, we’re using the Item model’s factory to create 50 fake items and insert them into the database.

  1. Run the Seeder: Finally, run the seeder command to populate your database with the fake data:
   php artisan db:seed --class=ItemSeeder

This command will execute the run() method inside the ItemSeeder class, which, in turn, will create 50 fake items in the database using the factory.

After running the seeder command, your database will be populated with fake data. You can adjust the number of items generated by modifying the count() method parameter in the seeder file.

This will generate a model file under the app directory.

  1. Prepare the controller: Create a controller to handle the Laravel pagination logic and data retrieval. Run the following command to generate a controller:
   php artisan make:controller ItemController

Open the ItemController file and import the necessary classes:


   namespace App\Http\Controllers;

   use App\Models\Item;
   use Illuminate\Http\Request;

Add a method to retrieve paginated data from the items table:

   public function index()
       $items = Item::paginate(10); // 10 items per page

       return view('items.index', compact('items'));

Here, we’re using the paginate() method provided by Laravel’s Eloquent ORM to retrieve paginated data.

Create a custom Laravel pagination view template: Run the following command to generate a custom pagination view template:

php artisan vendor:publish --tag=laravel-pagination

Create the index.blade.php file under the resources/views/items directory. Open the file and add the following code:

    <table class="table table-hover table-striped text-center">
            @foreach ($items as $item)
                    <td>{{ $item->id }}</td>
                    <td>{{ $item->name }}</td>
    <div class="pagination-container">
        {{ $items->render('vendor.pagination.bootstrap-5') }}

Here, we’re iterating over the $items collection and displaying the ID and name of each item in a table row. Additionally, we’re using the links() method to generate the Laravel pagination links at the bottom.

  1. Define routes: Open the routes/web.php file and add a route to the index() method of the ItemController:
   use App\Http\Controllers\ItemController;

   Route::get('/items', [ItemController::class, 'index']);
  1. Test the application: Start the development server:
 php artisan optimize
 php artisan serve

Visit `http://localhost:

Output Images:

laravel pagination

Our Recommendation

Avatar of Akhand Pratap Singh

Akhand Pratap Singh

Greetings and a warm welcome to my website! I am Akhand Pratap Singh, a dedicated professional web developer and passionate blogger.

Related Post

Leave a Comment


Subscribe for latest updates

We don't spam.