How to make a pagination of Laravel
"Learn how to set up pagination in Laravel with a step-by-step example."
Creating a Pagination with Laravel
Pagination is an essential part of web development and Laravel makes it easy to create a pagination. Pagination is used for organizing content into discrete pages, allowing users to navigate through the content with ease. Laravel provides a simple and convenient way to create pagination with its query builder and Eloquent ORM.
Creating pagination with Laravel is relatively simple. The first step is to create a query that will retrieve the data we want to paginate. We can do this with Eloquent's query builder:
$users = User::query()
->orderBy('name', 'asc')
->paginate(10);
The above query will retrieve all users, ordered by name in ascending order, and return a paginated result with 10 users per page. The paginated result will contain the following information:
- The current page number
- The total number of pages
- The total number of items
- The items for the current page
Once we have the query, we can render the pagination in our views. Laravel provides a number of methods for creating pagination links. We can use the simple “links” method to generate the HTML for the pagination links:
{{ $users->links() }}
The above code will generate HTML for the pagination links, allowing us to navigate through the different pages of results. We can also customize the pagination links by passing an options array to the links method. This options array can contain any of the following options:
- ‘route’ - The route to use for generating the links. If not specified, the current route will be used.
- ‘query’ - An array of query string values to append to the generated links.
- ‘fragment’ - A URL fragment to append to the generated links.
- ‘pageName’ - The name of the page query string variable.
- ‘page’ - The page number to use for generating the links. If not specified, the current page will be used.
- ‘onEachSide’ - The number of links to show on each side of the current page.
For example, we can specify the onEachSide option to generate more pagination links:
{{ $users->links('pagination::simple', ['onEachSide' => 3]) }}
This will generate pagination links with 3 links on each side of the current page. We can also customize the appearance of the pagination links by using our own custom view. For example, we can create a view with the following code:
@foreach ($items as $item)
{{ $item->name }}
@endforeach
{{ $paginator->links() }}
The above code will render the items, followed by the pagination links. The paginator will automatically detect the view and use it for generating the pagination links. This allows us to customize the appearance of the pagination links to match our application's design.
Creating pagination with Laravel is simple and convenient. With the query builder and Eloquent ORM, we can easily create a query to retrieve the data we want to paginate. We can then use the “links” method to generate the HTML for the pagination links, and customize the appearance of the links with our own custom view.