Pagination

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