0
0
Laravelframework~5 mins

Pagination in Laravel

Choose your learning style9 modes available
Introduction

Pagination helps split large lists of data into smaller pages. This makes it easier to read and faster to load.

Showing a list of blog posts on a website.
Displaying search results with many items.
Listing users or products in an admin panel.
Breaking down long tables into manageable pages.
Syntax
Laravel
$items = Model::paginate(10);

// In Blade view
{{ $items->links() }}
Use paginate(number) to get a set number of items per page.
Use links() in your Blade template to show page links.
Examples
Get 5 users per page and send to the view.
Laravel
$users = User::paginate(5);
return view('users.index', compact('users'));
Show each user name and the pagination links below.
Laravel
<!-- In Blade template -->
@foreach ($users as $user)
  <p>{{ $user->name }}</p>
@endforeach

{{ $users->links() }}
Get 15 published posts per page.
Laravel
$posts = Post::where('status', 'published')->paginate(15);
Sample Program

This controller gets 3 products per page. The Blade view shows product names and prices, then page links.

Laravel
<?php

namespace App\Http\Controllers;

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

class ProductController extends Controller
{
    public function index()
    {
        $products = Product::paginate(3);
        return view('products.index', compact('products'));
    }
}

// Blade file: resources/views/products/index.blade.php

/*
@foreach ($products as $product)
  <div>{{ $product->name }} - ${{ $product->price }}</div>
@endforeach

{{ $products->links() }}
*/
OutputSuccess
Important Notes

Pagination automatically reads the current page from the URL query (like ?page=2).

You can customize the pagination view if you want different styles.

Use simplePaginate() for simpler next/previous links without page numbers.

Summary

Pagination splits data into pages to improve usability and speed.

Use paginate() in your query and links() in your Blade view.

Laravel handles page numbers and URLs automatically.