Why Paginate?
Showing thousands of records on one page kills performance. Pagination breaks your data into manageable chunks, keeping your pages fast and your users happy.
Basic Pagination
Laravel's query builder makes pagination simple. Call paginate() on any query and specify how many items per page.
$users = User::paginate(15);
The result is a LengthAwarePaginator instance. It includes the current page's data, total records, and links to other pages.
Displaying Pagination Links
In your Blade view, render the pagination links with a single method call. Laravel handles the HTML for you.
<div class="container">
@foreach ($users as $user)
<p>{{ $user->name }}</p>
@endforeach
{{ $users->links() }}
</div>
You can customize the pagination view by passing a Blade view name to links().
Simple and Cursor Pagination
For large datasets, simplePaginate() skips counting total records for a speed boost. cursorPaginate() uses cursors for even better performance on massive tables.
$users = User::simplePaginate(20);
$users = User::cursorPaginate(20);
Simple pagination doesn't show the total page count. Cursor pagination is ideal for infinite scroll features.
Custom Pagination
You can manually create a paginator when working with arrays or collections. Use the LengthAwarePaginator class directly.
use Illuminate\Pagination\LengthAwarePaginator;
$items = collect(range(1, 50));
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$perPage = 10;
$sliced = $items->slice(($currentPage - 1) * $perPage, $perPage);
$paginator = new LengthAwarePaginator($sliced, $items->count(), $perPage, $currentPage);
This gives you full control over how data is sliced and presented.