Display Limited Records Per Page Using Laravel Pagination

Display Limited Records Per Page Using Laravel Pagination

The paginate method first counts the total number of records matched by the query before pulling the records from the database, so that when it retrieves records, the paginator knows how many pages of records there are. In this case, if your application doesn’t expect to show how many pages are available in your UI, you don’t really need a count query on records.

In this example, we passed one argument to the paginate method, which is the number of items you would like displayed “per page”. Here we are saying we want to display 5 items per page:

Hire laravel Developer

Steps to Display Limited Records Per Page Using Laravel Pagination:

Step 1: Create the Blog Controller

php artisan make:controller BlogController

Step 2: Define the Route (web.php)

use App\Http\Controllers\BlogController;
Route::get('/blog', [BlogController::class, 'index'])->name('blog.index');

Step 3: Fetch and Paginate Posts in Controller

namespace App\Http\Controllers;
use App\Models\Post;

class BlogController extends Controller
{
    public function index()
    {
        // Fetch latest posts, 5 per page
        $posts = Post::orderBy('created_at', 'desc')->paginate(5);

        // Return view with posts
        return view('blog.index', compact('posts'));
    }
}

Step 4: Create the Blade View

resources/views/blog/index.blade.php

@extends('layouts.app')
@section('content')
    <div class="container">
        <h1>Blog Posts</h1>
        @foreach ($posts as $post)
            <div class="card mb-3">
                <div class="card-body">
                    <h2 class="card-title">{{ $post->title }}</h2>
                    <p class="card-text">{{ Str::limit($post->body, 150) }}</p>
                    <a href="{{ route('posts.show', $post->id) }}" class="btn btn-primary">Read More</a>
                </div>
            </div>
        @endforeach
        <!-- Pagination Links -->
        <div class="d-flex justify-content-center">
            {{ $posts->links() }}
        </div>
    </div>
@endsection

Step 5: Enable Bootstrap Pagination (Optional)

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

Conclusion

Implementing pagination in Laravel is both simple and powerful thanks to its built-in support through Eloquent. By using the paginate() method, you can efficiently handle large datasets, improve load times, and enhance user experience with clean, navigable pages. Whether you’re building a blog, a product listing, or an admin dashboard, Laravel’s pagination tools offer the flexibility to integrate seamlessly with popular CSS frameworks like Bootstrap or Tailwind.

Laravel Development Services
Previous Article

Magento 2: How to Extend Tailwind CSS with Custom Classes in Hyvä Theme

Next Article

What is Adobe Experience Manager (AEM)?

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨