Skip to main content
Pagination Customizations

Pagination Customizations

Pagination splits large result sets into pages. Laravel provides offset pagination, simple pagination without a total count, and cursor pagination for large or frequently changing data.

Offset pagination

$posts = Post::latest()->paginate(15);

return view('posts.index', compact('posts'));

In Blade, render links and the page range:

@foreach ($posts as $post)
    <article>{{ $post->title }}</article>
@endforeach

{{ $posts->links() }}

Offset pagination runs a count query and a data query. It gives you a total page count, which is useful for numbered navigation.

Simple pagination

$posts = Post::latest()->simplePaginate(15);

Simple pagination skips the count query and shows previous/next links only. It is faster on large tables where a total is not required.

Cursor pagination

$posts = Post::orderBy('id')->cursorPaginate(15);

Cursor pagination uses an encoded position rather than an offset. It stays stable when rows are inserted and avoids expensive offsets, which makes it a good fit for APIs and infinite scroll.

Preserving query strings

$posts = Post::query()
    ->when($request->filled('q'), fn ($q) => $q->where('title', 'like', '%'.$request->q.'%'))
    ->paginate(15)
    ->withQueryString();

Without withQueryString(), moving to page two drops active filters. Use appends() for additional values you want in the links.

$posts->appends(['sort' => 'title']);

Customizing the page size

$perPage = min(max((int) $request->query('per_page', 15), 5), 50);

$posts = Post::paginate($perPage);

Clamp user-provided page sizes. An unchecked value can turn one request into a heavy query.

Custom pagination views

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

This publishes the default views so you can restyle them. Choose a view per paginator:

{{ $posts->links('pagination::tailwind') }}

Pagination in JSON

{
    "data": [ ... ],
    "links": { "first": "...", "prev": null, "next": "..." },
    "meta": {
        "current_page": 1,
        "last_page": 8,
        "per_page": 15,
        "total": 112
    }
}

API resources preserve pagination metadata automatically when you return a paginated collection inside a resource collection.

Choosing a paginator

  • Offset for admin tables with page numbers and a known total.
  • Simple for large lists where only next/previous matters.
  • Cursor for feeds, APIs, and data that changes while users browse.