Pagination — paginate() & simplePaginate()
Laravel's built-in pagination handles everything — generating SQL LIMIT/OFFSET, providing page navigation links, and preserving query parameters. paginate() shows page numbers, simplePaginate() shows only Previous/Next (more efficient for large datasets).
Pagination
// Controller
public function index()
{
$posts = Post::latest()->paginate(15); // 15 per page
// OR
$posts = Post::latest()->simplePaginate(15); // Prev/Next only
return view('posts.index', compact('posts'));
}
// Blade — render pagination links
<div>
@foreach ($posts as $post)
<h2>{{ $post->title }}</h2>
@endforeach
{{ $posts->links() }} {{-- Pagination navigation --}}
</div>
// Preserve query parameters in pagination links
{{ $posts->withQueryString()->links() }}
// Page links include ?search=laravel&page=2
// Custom pagination view
{{ $posts->links('vendor.pagination.custom') }}
// API pagination (JSON)
return Post::paginate(15);
// Returns: data, current_page, per_page, total, linksTip
Tip
Practice Pagination paginate simplePaginate in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Define the relationship, Eloquent builds the SQL automatically
Practice Task
Note
Practice Task — (1) Write a working example of Pagination paginate simplePaginate from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Pagination paginate simplePaginate is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.