You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -53,14 +56,48 @@ In this example, the only argument passed to the `paginate` method is the number
53
56
}
54
57
55
58
<aname="simple-pagination"></a>
56
-
#### "Simple Pagination"
59
+
#### Simple Pagination
57
60
58
61
The `paginate` method counts the total number of records matched by the query before retrieving the records from the database. This is done so that the paginator knows how many pages of records there are in total. However, if you do not plan to show the total number of pages in your application's UI then the record count query is unnecessary.
59
62
60
63
Therefore, if you only need to display simple "Next" and "Previous" links in your application's UI, you may use the `simplePaginate` method to perform a single, efficient query:
61
64
62
65
$users = DB::table('users')->simplePaginate(15);
63
66
67
+
<aname="cursor-pagination"></a>
68
+
#### Cursor Pagination
69
+
70
+
While `paginate` and `simplePaginate` create queries using `offset`, cursor pagination works by constructing `where` clauses that compare the values of the ordered columns. This can be really helpful for large data-sets, infinite scrolling and APIs.
71
+
72
+
Similar to `simplePaginate`, `cursorPaginate` displays "Next" and "Previous" links in your application's UI. You may use the `cursorPaginate` method like so:
An order by clause is required for cursor pagination to work for the Database Query Builder.
77
+
78
+
<aname="cursor-vs-offset-pagination"></a>
79
+
### Cursor vs Offset Pagination
80
+
81
+
To illustrate the difference between offset pagination and cursor pagination, mentioned below are the SQL queries constructed by both to view the "second page" of a `users` table ordered by `id`:
82
+
83
+
```sql
84
+
# Offset Pagination
85
+
select*from users order by id asclimit15 offset 15;
86
+
87
+
# Cursor Pagination
88
+
select*from users where id >15order by id asclimit15;
89
+
```
90
+
91
+
Cursor pagination offers the following advantages over offset pagination:
92
+
1. For large data-sets, cursor pagination will perform better if the "order by" columns are indexed. This is because offset scans through all the previous data unlike comparison queries.
93
+
2. For data-sets with frequent writes, offset pagination may skip records or show duplicates if results have been added to or deleted from the previous page.
94
+
95
+
Cursor pagination, however, has the following limitations:
96
+
1. Like `simplePaginate`, it can only be used to display "Next" and "Previous" links and does not support page numbers.
97
+
2. It requires that the ordering is based on at least one unique column (or a combination of columns that are unique).
98
+
3. It requires that the "order by" directions (desc/asc) are the same if there are multiple "order by" clauses.
99
+
4. Query expressions in "order by" clauses are supported only if they are aliased and added to the select clause as well.
100
+
64
101
<aname="paginating-eloquent-results"></a>
65
102
### Paginating Eloquent Results
66
103
@@ -78,14 +115,47 @@ You may also use the `simplePaginate` method when paginating Eloquent models:
Offset pagination determines which items to show using the page number. Cursor pagination instead uses what is called a "cursor" to determine which items to display.
126
+
127
+
The cursor is an instance of `Illuminate\Pagination\Cursor`, which includes values that identify a specific record, from where to start paginating, along with the direction to paginate.
128
+
129
+
So, for instance, if we were paginating records from the `users` table in ascending order of the `id`, a cursor can be instantiated like so:
130
+
131
+
```php
132
+
use Illuminate\Pagination\Cursor;
133
+
134
+
$cursor = new Cursor(['id' => 10], true);
135
+
```
136
+
137
+
The first argument of the constructor identifies the record after which we need to start paginating and the second argument refers to the direction (`true` being forwards). The cursor above indicates that the pagination should start from an `id` greater than 10.
138
+
139
+
The `cursorPaginate` methods on the Eloquent and Database query builders will automatically instantiate the cursors for the next and previous pages, json encode them, and then base64 safe URL encode the values as a query parameter.
140
+
141
+
However, in case you need to manually supply a cursor, you may do so using the `cursorPaginate` method on the Eloquent or Database query builder:
Sometimes you may wish to create a pagination instance manually, passing it an array of items that you already have in memory. You may do so by creating either an `Illuminate\Pagination\Paginator`or `Illuminate\Pagination\LengthAwarePaginator` instance, depending on your needs.
154
+
Sometimes you may wish to create a pagination instance manually, passing it an array of items that you already have in memory. You may do so by creating either an `Illuminate\Pagination\Paginator`, `Illuminate\Pagination\LengthAwarePaginator`or `Illuminate\Pagination\CursorPaginator` instance, depending on your needs.
85
155
86
-
The `Paginator`class does not need to know the total number of items in the result set; however, because of this, the class does not have methods for retrieving the index of the last page. The `LengthAwarePaginator` accepts almost the same arguments as the `Paginator`; however, it requires a count of the total number of items in the result set.
156
+
The `Paginator`and `CursorPaginator` classes do not need to know the total number of items in the result set; however, because of this, these classes do not have methods for retrieving the index of the last page. The `LengthAwarePaginator` accepts almost the same arguments as the `Paginator`; however, it requires a count of the total number of items in the result set.
87
157
88
-
In other words, the `Paginator` corresponds to the `simplePaginate` method on the query builder, while the `LengthAwarePaginator` corresponds to the `paginate` method.
158
+
In other words, the `Paginator` corresponds to the `simplePaginate` method on the query builder, the `CursorPaginator` corresponds to the `cursorPaginate` method and the `LengthAwarePaginator` corresponds to the `paginate` method.
89
159
90
160
> {note} When manually creating a paginator instance, you should manually "slice" the array of results you pass to the paginator. If you're unsure how to do this, check out the [array_slice](https://secure.php.net/manual/en/function.array-slice.php) PHP function.
91
161
@@ -133,7 +203,9 @@ If you need to append a "hash fragment" to URLs generated by the paginator, you
133
203
<aname="displaying-pagination-results"></a>
134
204
## Displaying Pagination Results
135
205
136
-
When calling the `paginate` method, you will receive an instance of `Illuminate\Pagination\LengthAwarePaginator`. When calling the `simplePaginate` method, you will receive an instance of `Illuminate\Pagination\Paginator`. These objects provide several methods that describe the result set. In addition to these helpers methods, the paginator instances are iterators and may be looped as an array. So, once you have retrieved the results, you may display the results and render the page links using [Blade](/docs/{{version}}/blade):
206
+
When calling the `paginate` method, you will receive an instance of `Illuminate\Pagination\LengthAwarePaginator`. When calling the `simplePaginate` method, you will receive an instance of `Illuminate\Pagination\Paginator`. When calling the `cursorPaginate` method, you will receive an instance of `Illuminate\Pagination\CursorPaginator`.
207
+
208
+
These objects provide several methods that describe the result set. In addition to these helpers methods, the paginator instances are iterators and may be looped as an array. So, once you have retrieved the results, you may display the results and render the page links using [Blade](/docs/{{version}}/blade):
137
209
138
210
```html
139
211
<divclass="container">
@@ -248,7 +320,7 @@ Laravel includes pagination views built using [Bootstrap CSS](https://getbootstr
248
320
}
249
321
250
322
<aname="paginator-instance-methods"></a>
251
-
## Paginator Instance Methods
323
+
## Paginator and LengthAwarePaginator Instance Methods
252
324
253
325
Each paginator instance provides additional pagination information via the following methods:
254
326
@@ -272,3 +344,26 @@ Method | Description
272
344
`$paginator->url($page)` | Get the URL for a given page number.
273
345
`$paginator->getPageName()` | Get the query string variable used to store the page.
274
346
`$paginator->setPageName($name)` | Set the query string variable used to store the page.
347
+
348
+
<aname="cursor-paginator-instance-methods"></a>
349
+
## Cursor Paginator Instance Methods
350
+
351
+
Each cursor paginator instance provides additional pagination information via the following methods:
352
+
353
+
Method | Description
354
+
------- | -----------
355
+
`$paginator->count()` | Get the number of items for the current page.
356
+
`$paginator->cursor()` | Get the current cursor instance.
357
+
`$paginator->getOptions()` | Get the paginator options.
358
+
`$paginator->hasPages()` | Determine if there are enough items to split into multiple pages.
359
+
`$paginator->hasMorePages()` | Determine if there are more items in the data store.
360
+
`$paginator->getCursorName()` | Get the query string variable used to store the cursor.
361
+
`$paginator->items()` | Get the items for the current page.
362
+
`$paginator->nextCursor()` | Get the cursor instance for the next set of items.
363
+
`$paginator->nextPageUrl()` | Get the URL for the next page.
364
+
`$paginator->onFirstPage()` | Determine if the paginator is on the first page.
365
+
`$paginator->perPage()` | The number of items to be shown per page.
366
+
`$paginator->previousCursor()` | Get the cursor instance for the previous set of items.
367
+
`$paginator->previousPageUrl()` | Get the URL for the previous page.
368
+
`$paginator->setCursorName()` | Set the query string variable used to store the cursor.
369
+
`$paginator->url($cursor)` | Get the URL for a given cursor instance.
0 commit comments