Laravel Pagination Tutorial – Step By Step Guide With Example

Laravel Pagination Tutorial – Step by Step Guide with Example
Avinash Chaurasiya Sep 10, 2025 56
Pagination,SQL Query Slow,how To Fix Where Query Is To Slow,pagination In Laravel

Laravel Pagination Tutorial – Step by Step Guide with Example


Laravel Pagination Tutorial – Step by Step Guide with Example

When building web applications, handling large amounts of data efficiently is crucial. Instead of showing thousands of records at once, pagination helps split data into manageable pages. Laravel makes pagination simple with its built-in paginate() method.

🔹 Step 1: Setup Your Laravel Project

Make sure you have a Laravel project ready. If not, create one:

composer create-project laravel/laravel blog-app

🔹 Step 2: Create a Blog Model and Migration

Create a model for blog posts:

php artisan make:model Blog -m

Edit the migration file:


Schema::create('blogs', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('content');
    $table->timestamps();
});
  

Run migration:

php artisan migrate

🔹 Step 3: Add Dummy Data with Seeder

php artisan make:seeder BlogSeeder

Edit BlogSeeder.php:


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

class BlogSeeder extends Seeder
{
    public function run()
    {
        for ($i = 1; $i <= 50; $i++) {
            Blog::create([
                'title' => "Sample Blog Post $i",
                'content' => "This is the content of blog post number $i."
            ]);
        }
    }
}
  

Run seeder:

php artisan db:seed --class=BlogSeeder

🔹 Step 4: Create a Controller

php artisan make:controller BlogController

Edit BlogController.php:


use App\Models\Blog;

class BlogController extends Controller
{
    public function index()
    {
        // Fetch paginated blogs (5 per page)
        $blogs = Blog::paginate(5);
        return view('blogs.index', compact('blogs'));
    }
}
  

🔹 Step 5: Add Route


use App\Http\Controllers\BlogController;

Route::get('/blogs', [BlogController::class, 'index']);
  

🔹 Step 6: Create Blade View

Create resources/views/blogs/index.blade.php:


@extends('layouts.app')

@section('content')
    <div class="container">
        <h2>Blog Posts</h2>

        @foreach($blogs as $blog)
            <div class="card mb-3">
                <div class="card-body">
                    <h4>{{ $blog->title }}</h4>
                    <p>{{ $blog->content }}</p>
                </div>
            </div>
        @endforeach

        <!-- Pagination Links -->
        {{ $blogs->links() }}
    </div>
@endsection
  

🔹 Step 7: Customizing Pagination Style

By default, Laravel uses Tailwind CSS for pagination. If you want Bootstrap:


{{ $blogs->links('pagination::bootstrap-5') }}
  

If you want use pagination::bootstrap-5 global then you need to boot your library:


namespace App\Providers;

use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Paginator::useBootstrapFive();
    }
}

After this you can use like :


{{ $blogs->links() }}

🎯 Final Output

You will now see blog posts displayed with pagination at the bottom. Clicking on page numbers will fetch the next set of blog posts automatically.

✅ Conclusion

Laravel’s pagination system is very easy to use and flexible. You can paginate any model, customize the view, and even use AJAX for advanced functionality. Now you can efficiently manage large datasets in your Laravel applications.

WhatsApp Us