Pagination

Contents
Paginate queries
Render links
Simple paginate
Paginate APIs

Paginate queries

The paginate method slices a query into pages and counts the total rows for you. Pass the page size you want, and Laravel reads the current page number from the request on its own.

$query = User::query(); $users = $query->paginate(15); echo $users->total();

Render links

The result object knows how to draw its own page buttons. The links method returns ready HTML with previous, numbered, and next buttons that keep existing filters in the address.

$query = User::query(); $users = $query->paginate(15); foreach ($users as $user) { echo $user->name; } echo $users->links();

Simple paginate

Counting every row costs time on giant tables, so simplePaginate skips the total. It still renders previous and next buttons, which is all endless lists and feeds need.

$users = User::query()->simplePaginate(15); foreach ($users as $user) { echo $user->name; }

Paginate APIs

API routes can return the paginator directly because it serializes to JSON with data and meta blocks. Clients read the next page address from the payload and follow it.

Route::get("/users", function () { return User::query()->paginate(10); });

Next: File uploads

Article author: Arthur Isaev

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: