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 withgroupBy
. 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:
1 2 | <span class="token variable">$users</span> <span class="token operator">=</span> App <span class="token package">User</span> <span class="token punctuation">:</span> <span class="token punctuation">:</span> <span class="token function">paginate</span> <span class="token punctuation">(</span> <span class="token number">15</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> |
You can also call paginate
after adding the conditions to the query:
1 2 | <span class="token variable">$users</span> <span class="token operator">=</span> User <span class="token punctuation">:</span> <span class="token punctuation">:</span> <span class="token function">where</span> <span class="token punctuation">(</span> <span class="token single-quoted-string string">'votes'</span> <span class="token punctuation">,</span> <span class="token single-quoted-string string">'>'</span> <span class="token punctuation">,</span> <span class="token number">100</span> <span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">paginate</span> <span class="token punctuation">(</span> <span class="token number">15</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> |
You can also use the simplePaginate
method in Eloquent models:
1 2 3 | <span class="token variable">$users</span> <span class="token operator">=</span> User <span class="token punctuation">:</span> <span class="token punctuation">:</span> <span class="token function">where</span> <span class="token punctuation">(</span> <span class="token single-quoted-string string">'votes'</span> <span class="token punctuation">,</span> <span class="token single-quoted-string string">'>'</span> <span class="token punctuation">,</span> <span class="token number">100</span> <span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">simplePaginate</span> <span class="token punctuation">(</span> <span class="token number">15</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> |
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: https://stackoverflow.com/questions/43601095/laravel-5-4-lengthawarepaginator
Consider the example:
1 2 3 4 5 6 7 8 9 | <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">filterModels</span> <span class="token punctuation">(</span> <span class="token variable">$page</span> <span class="token punctuation">,</span> <span class="token variable">$perPage</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$query</span> <span class="token operator">=</span> User <span class="token punctuation">:</span> <span class="token punctuation">:</span> <span class="token function">newQuery</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token punctuation">.</span> <span class="token variable">$query</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">LengthAwarePaginator</span> <span class="token punctuation">(</span> <span class="token variable">$query</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">forPage</span> <span class="token punctuation">(</span> <span class="token variable">$page</span> <span class="token punctuation">,</span> <span class="token variable">$perPage</span> <span class="token punctuation">)</span> <span class="token punctuation">,</span> <span class="token variable">$collection</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">count</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">,</span> <span class="token variable">$perPage</span> <span class="token punctuation">,</span> <span class="token variable">$page</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> |
2. The parameters returned in the Paginator
These parameters you can refer to when designing the API or using with other frameworks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <span class="token punctuation">{</span> <span class="token property">"total"</span> <span class="token operator">:</span> <span class="token number">50</span> <span class="token punctuation">,</span> // tổng số bản ghi <span class="token property">"per_page"</span> <span class="token operator">:</span> <span class="token number">15</span> <span class="token punctuation">,</span> // số bản ghi trên <span class="token number">1</span> trang <span class="token property">"current_page"</span> <span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">,</span> // trang hiện tại <span class="token property">"last_page"</span> <span class="token operator">:</span> <span class="token number">4</span> <span class="token punctuation">,</span> // trang cuối cùng <span class="token property">"first_page_url"</span> <span class="token operator">:</span> <span class="token string">"http://laravel.app?page=1"</span> <span class="token punctuation">,</span> <span class="token property">"last_page_url"</span> <span class="token operator">:</span> <span class="token string">"http://laravel.app?page=4"</span> <span class="token punctuation">,</span> <span class="token property">"next_page_url"</span> <span class="token operator">:</span> <span class="token string">"http://laravel.app?page=2"</span> <span class="token punctuation">,</span> <span class="token property">"prev_page_url"</span> <span class="token operator">:</span> <span class="token null">null</span> <span class="token punctuation">,</span> <span class="token property">"path"</span> <span class="token operator">:</span> <span class="token string">"http://laravel.app"</span> <span class="token punctuation">,</span> <span class="token property">"from"</span> <span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">,</span> // hiên thị từ bản ghi số <span class="token number">1</span> trong database <span class="token property">"to"</span> <span class="token operator">:</span> <span class="token number">15</span> <span class="token punctuation">,</span> // hiển thị đến bản ghi số <span class="token number">15</span> trong database <span class="token property">"data"</span> <span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token punctuation">{</span> // Result Object <span class="token punctuation">}</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> // Result Object <span class="token punctuation">}</span> <span class="token punctuation">]</span> <span class="token punctuation">}</span> |
3. Paginator Instance Methods
In addition, Paginator provides many methods for you to use:
Method | description |
---|---|
$ 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. |
summary
- Laravel supports two methods for
simplePaginate
:paginate
andsimplePaginate
with two instances returningIlluminatePaginationLengthAwarePaginator
andIlluminatePaginationPaginator
for both query builder and Eloquent. - Paginate does not support
groupBy
, if you want to usegroupBy
, 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 https://laravel.com/docs/6.x/pagination#paginator-instance-methods