Laravel 11 Pagination Example |
Laravel 11 Pagination Example |

Laravel 11 Pagination Example


Pagination is a crucial aspect of web development, especially when dealing with large datasets. Laravel, a popular PHP framework, offers built-in pagination support to streamline the process of dividing content into manageable chunks. In this tutorial, we will walk through a practical example of implementing pagination in Laravel 11.

Step 1:

Setting Up Laravel Project Before diving into pagination, ensure you have Laravel installed on your system. If not, you can install it using Composer by running the following command:

composer create-project --prefer-dist laravel/laravel pagination-example

Navigate to the project directory:

cd pagination-example

Step 2:

Database Configuration For demonstration purposes, let’s assume we have a database table named “products” with columns id, name, price, and description. Configure your database credentials in the .env file:


After configuring the database, run migrations to create the necessary tables:

php artisan migrate

Step 3:

Generating Model and Seeder Next, let’s generate a model and a seeder for the products table:

php artisan make:model Product -m

This command will generate a Product model and its corresponding migration file. Open the migration file and define the schema for the products table:

// database/migrations/create_products_table.php

public function up()
Schema::create('products', function (Blueprint $table) {
$table->decimal('price', 8, 2);

Now, run the migration to create the products table:

php artisan migrate

Next, generate a seeder to populate the products table with dummy data:

php artisan make:seeder ProductSeeder

Open the ProductSeeder.php file located in the database/seeders directory and define the logic to insert dummy data:

// database/seeders/ProductSeeder.php

public function run()

Now, run the seeder to populate the products table:

php artisan db:seed --class=ProductSeeder

Step 4:

Creating Pagination Logic With the database setup complete, let’s implement pagination in our Laravel application. Open the ProductController.php file located in the app/Http/Controllers directory and add the following method:

// app/Http/Controllers/ProductController.php

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

public function index()
$products = Product::paginate(10); // Change '10' to adjust the number of items per page
return view('products.index', compact('products'));

Step 5:

Displaying Paginated Data Now, let’s create a blade view to display the paginated data. Create a new file named index.blade.php in the resources/views/products directory:

<!-- resources/views/products/index.blade.php -->

@foreach($products as $product)
<h2>{{ $product->name }}</h2>
<p>{{ $product->description }}</p>
<strong>Price: ${{ $product->price }}</strong>

{{ $products->links() }}

Step 6:

Testing Pagination To test our pagination implementation, navigate to the following URL in your web browser:


You should see a list of products with pagination links at the bottom. Click on the pagination links to navigate between pages.


Congratulations! You’ve successfully implemented pagination in Laravel 11. Pagination is a powerful feature that improves the user experience by breaking down large datasets into smaller, more manageable chunks. With the knowledge gained from this tutorial, you can now implement pagination in your Laravel applications with ease. Happy coding!