SyntaxStudy
Sign Up
PHP Beginner 4 min read

API Pagination

Pagination

Laravel's paginate() returns consistent pagination metadata. Use cursorPaginate() for better performance on large datasets.

Example
// Offset pagination
$users = User::latest()->paginate(15);
return UserResource::collection($users);
// Response includes links, meta.total, meta.per_page, meta.current_page
// Cursor pagination (fast for large tables)
$posts = Post::orderBy("id")->cursorPaginate(20);
// GET /api/posts?cursor=eyJpZCI6MTAwfQ
// Custom page size with cap
$perPage = min((int)request("per_page", 15), 100);
$users = User::paginate($perPage);
Pro Tip

Cap the maximum per_page value to prevent clients from requesting millions of rows in one request.