Hello, everyone! Have you ever visited a website that had an excessive amount of content on a single page? Maybe it includes a list of YouTube videos, blog posts, or online products. If I’m not wrong, probably it looked very messy, right?
That’s where pagination comes in!
Pagination means breaking up big lists into smaller, easy-to-read pages — just like your favorite magazine has pages 1, 2, 3, and so on!
In this blog, we are going to learn how to use Laravel pagination in the easiest way.
What is Pagination?
Suppose you have 100 blog posts, and showing all 100 at once will make the page heavy and slow!
With pagination, you can display 10 at a time and give people buttons like
<< Prev | 1 | 2 | 3 | Next >>
Laravel makes this super easy and simple without doing any of the hard work.
Step 1: Create a Laravel App
Still, you don’t have a project right now; make one:
bash composer create-project laravel/laravel laravel-pagination-demo cd laravel-pagination-demo php artisan serve
Step 2: Make a Model and Migration
Think we’re making a blog.
Create a Post model and migration:
bash php artisan make:model Post -m
In the migration file (database/migrations/…create_posts_table.php), add:
php Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->timestamps(); });
Run the migration:
bash php artisan migrate
Step 3: Seed Some Dummy Data
To test pagination, let’s include 50 fake posts.
Install Faker using Laravel’s seeder. If you want to know more about laravel seeding read this.
bash php artisan make:seeder PostSeeder
Open the PostSeeder.php file and add:
php use App\Models\Post; use Illuminate\Support\Str; public function run() { for ($i = 1; $i <= 50; $i++) { Post::create([ 'title' => "Post Title $i", 'body' => Str::random(100), ]); } }
Update the DatabaseSeeder.php file:
php public function run(): void { $this->call(PostSeeder::class); }
Now run the seeder:
bash php artisan db:seed
Congrats! Now you have 50 posts in your database!
Step 4: Display Posts With Pagination
Now, show those posts on a webpage — with pagination!
In routes/web.php, add:
php use App\Models\Post; Route::get('/posts', function () { $posts = Post::paginate(10); // 10 posts per page return view('posts', compact('posts')); });
Now create the view file: resources/views/posts.blade.php
blade <!DOCTYPE html> <html> <head> <title>Paginated Posts</title> </head> <body> <h1>All Blog Posts</h1> @foreach ($posts as $post) <div style="margin-bottom: 20px;"> <h3>{{ $post->title }}</h3> <p>{{ $post->body }}</p> </div> @endforeach <!-- Pagination links --> {{ $posts->links() }} </body> </html>
Go to http://localhost:8000/posts
You’ll see now 10 posts per page — with automatic “Next,” “Previous,” and page number links!
Bonus Tip: Use Bootstrap Styling
To make your pagination look pretty, Laravel supports Bootstrap out of the box.
First, open AppServiceProvider.php and add this inside the boot() method:
php use Illuminate\Pagination\Paginator; public function boot(): void { Paginator::useBootstrapFive(); }
Then install Bootstrap via CDN in your view (resources/views/posts.blade.php):
html <head> <title>Paginated Posts</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> </head>
Presently the pagination links will look tidy and fashionable!
Bonus Features You Can Use
Simple Pagination (Just “Next” and “Previous”)
php $posts = Post::simplePaginate(10);
hanging the Page URL Name
php $posts = Post::paginate(10, ['*'], 'my_page');
Show a Custom Message If There Are No Posts
blade @forelse ($posts as $post) <h3>{{ $post->title }}</h3> @empty <p>No posts found!</p> @endforelse
Recap—What Did You Learn?
What pagination is (breaking lists into pages)
How to use paginate() in Laravel
How to show page links in Blade
How to style them with Bootstrap
How to use simplePaginate() for basic navigation
Read More Bolg Posts
Laravel Migrations: Manage Your Database Migrations with Our Simple Guide
How to use Laravel Queues Beginner’s Guide
Tips and Tricks for Running Commands with Laravel Process
You Did It!
Now you have learned how to paginate anything in Laravel — blog posts, users, products, anything!
You’re now able to build real apps like a pro.
Next steps:
- Add pagination to your user list
- Use filters/search + pagination together
- Make an API with paginated results