Laravel Pagination: Beyond the Basics
Laravel Pagination: Beyond the Basics
Laravel Pagination: Beyond the Basics
Tip: Simple Pagination
$posts = Post::simplePaginate(10);
Uses LIMIT/OFFSET without counting total rows. Faster for large datasets.
Gotcha: Cursor Pagination
$posts = Post::cursorPaginate(10);
No OFFSET — uses a cursor (ID) for the next page. Much faster for deep pagination.
Tip: Custom Pagination View
$posts->links('vendor.pagination.custom');
Publish the default views first: php artisan vendor:publish --tag=laravel-pagination.
Gotcha: Pagination with Query Strings
$posts->appends(['sort' => 'date'])->links();
Preserves query parameters across pages.
Tip: onEachSide() for More Page Links
$posts->onEachSide(2)->links();
Shows 2 page links on each side of the current page.
Gotcha: Manual Pagination
use Illuminate\Pagination\Paginator;
$paginator = new Paginator($items, $perPage, $currentPage);
Tip: Use route:cache Carefully
php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.
Tip: Model APP_KEY Rotation
Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.
Gotcha: Local Scope Leaks
Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.
Senior Insight
Pagination seems simple until you need cursor-based pagination for a real-time feed. Laravel's simplePaginate() is great for UI, but it breaks down when rows are inserted or deleted between page loads — users see duplicates or miss entries. For infinite scroll or API feeds, I always use cursor pagination. The implementation takes a bit more code, but the UX difference is dramatic.
Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)