Pagination in Laravel

Tram Ho

This article I will mention the use of paging in Laravel

1. Basic Usage

1.1. Paginating Query Builder Results

There are several ways to paginate items. The simple way is to use the paginate method in the query builder or the Eloquent query . This method automatically sets the limits and the offset based on the current page. By default, the current page will be used based on the value of the page in the query string argument from the HTTP Request.

Note: Currently, the paginate operator cannot work with groupBy . If you need to use groupBy during paginate, we recommend that you create your own paginator, which will be covered later.

“Simple Pagination”

If you want to simply display “Next” and “Previews” links on pagiantion view, you can use the simplePaginate method to make a query more efficient.

1.2. Paginating Eloquent Results

You can also use paginate in Eloquent queries:

You can also call paginate after adding the conditions to the query:

You can also use the simplePaginate method in Eloquent models:

1.3. Manually Creating A Paginator

Sometimes you want to create pagination instances yourself, passing it into an array of items. You can do this by creating an IlluminatePaginationPaginator or IlluminatePaginationLengthAwarePaginator instance based on your needs.

The Paginator class Paginator not need to know the total number of records in the result set, but for that reason it cannot get the index of the final page. The LengthAwarePaginator class accepts almost the same parameters as the Paginator but it requires a total number of records ?

In other words, Paginator corresponds to simplePaginate method in query builder and Eloquent while LengthAwarePaginator corresponds to paginate method.

Note: When you create your own pagination, you should “slice” the array of results you pass into the pagination. If you don’t know how, try using the array_slice PHP function

=> you need to paginate the results of the items, the other class only adds paginate information for you only

For example:

Consider the example:

2. The parameters returned in the Paginator

These parameters you can refer to when designing the API or using with other frameworks

3. Paginator Instance Methods

In addition, Paginator provides many methods for you to use:

$ results-> count ()Get the number of items for the current page.
$ results-> currentPage ()Get the current page number.
$ results-> firstItem ()Get the number of results for the first item.
$ results-> getOptions ()Get the pagination options.
$ results-> getUrlRange ($ start, $ end)Create a series of pagination URLs.
$ results-> hasMorePages ()Determine if there are enough items to divide into multiple pages.
$ results-> items ()Get the entries for the current page
$ results-> lastItem ()Get the result number for the last item in the result. Get the result number of the last item in the results.
$ results-> lastPage ()Get the page number of the last available page (but this is not available when using simplePaginate )
$ results-> nextPageUrl ()Get the URL for the next page.
$ results-> onFirstPage ()Determine if paginator is on the first page.
$ results-> perPage ()The number of items will be displayed on each page.
$ results-> previousPageUrl ()Get the URL for the previous page.
$ results-> total ()Determine the total number of matching items in the data store. (This is also not available when using simplePaginate )
$ results-> url ($ page)Get URLs for certain pages.
$ results-> getPageName ()Get the query string variable used to store the page.
$ results-> setPageName ($ name)Sets query string variables used to store pages.


  • Laravel supports two methods for simplePaginate : paginate and simplePaginate with two instances returning IlluminatePaginationLengthAwarePaginator and IlluminatePaginationPaginator for both query builder and Eloquent.
  • Paginate does not support groupBy , if you want to use groupBy , you should build your own paginator.
  • When manually building Paginate using Laravel instances, please pass the items on to the paginated result. ? . The LengthAwarePaginator class has the added effect of paging information for you.

I refer to it via

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo