What is pagination
Laravel’s pagination is integrated with the query builder and Eloquent ORM, so you can start using it without any configuration. The current page is automatically detected from thepage query parameter on the HTTP request and is also automatically appended to the generated links.
The default HTML supports Tailwind CSS, and Bootstrap CSS is available as an option.
Three pagination methods
Basic usage
Query builder pagination
Eloquent pagination
simplePaginate
When you don’t need a total-count query (displaying only “Previous” and “Next” links),simplePaginate() is more efficient.
cursorPaginate (cursor pagination)
Cursor pagination uses a WHERE clause instead of OFFSET, delivering high performance on large datasets. It’s especially well-suited to infinite-scroll UIs.OFFSET vs cursor comparison
Controller implementation
Displaying pagination links in Blade
links() method automatically generates HTML for the page links. It shows three pages before and three pages after the current page.
Adjusting the number of visible links
UseonEachSide() to change how many page links to show before and after the current page.
Accepting per-page count from the request
Displaying multiple paginators on one page
When displaying two paginators on the same screen, both usingpage conflicts. Change the parameter name via the third argument.
Customizing URLs
Changing the base URL
Appending query parameters
Appending a hash fragment
API responses (JSON output)
If you return a paginator directly from a route or controller, it’s automatically converted to JSON.Combining with API resources
To wrap apaginate() result with an API resource collection, pass it to UserResource::collection().
UserResource::collection(), pagination information is automatically added as metadata.
The JSON for
cursorPaginate() includes next_cursor and prev_cursor instead of page numbers. API clients use these values as the cursor parameter on the next request.Custom pagination views
Specifying a view file directly
Changing the default view to a custom file
First publish the official views and then customize.resources/views/vendor/pagination/:
tailwind.blade.php— Default (for Tailwind CSS)bootstrap-5.blade.php— For Bootstrap 5simple-tailwind.blade.php— For simplePaginate- …
tailwind.blade.php directly, or create a new view and specify it in AppServiceProvider.
Using Bootstrap CSS
To use Bootstrap instead of Tailwind, configure it inAppServiceProvider’s boot().
Manually creating a paginator
To apply pagination to existing data such as an array, instantiate the paginator class directly.Commonly used instance methods
Summary
How to choose a pagination method
How to choose a pagination method
paginate()— when you need total count and numbered page links (typical list screens)simplePaginate()— when “Previous” and “Next” only is sufficient (faster)cursorPaginate()— for large datasets, infinite scroll, or when data is written frequently (highest performance)
Basic Blade display pattern
Basic Blade display pattern
paginate() to the view and use links() to output page links.
The current page is automatically detected from the page query parameter.Pagination in APIs
Pagination in APIs
Returning a paginator directly from a route automatically converts it to JSON.
To combine with API resources, return
UserResource::collection($paginator).
The response includes data (an array of records) and various metadata fields.