@@ -66,7 +66,6 @@ This value is automatically detected by the package taking your custom config in
6666public function index()
6767{
6868 $users = DB::table('users')->cursorPaginate();
69-
7069 return $users;
7170}
7271````
@@ -95,6 +94,81 @@ $users = User::orderBy('id', 'desc')->cursorPaginate(15);
9594Don't worry, the package will detect if the model's primary key is being used for sorting, and it will adapt
9695itself so the next and previous work as expected.
9796
97+ ### Identifier
98+
99+ The paginator identifier is basically the cursor attribute. The model's attribute that will be used for paginating.
100+ It's really important that this identifier is ** unique** on the results. Duplicated identifiers could cause that some
101+ records are not showed, so be careful.
102+
103+ * If the query is not sorted by the identifier, the cursor pagination might not work as expected.*
104+
105+ #### Auto-detecting Identifier
106+
107+ If no identifier is defined, the cursor will try to figure it out by itself. First, it will check if there's any orderBy clause.
108+ If there is, it will take the ** first column** that is sorted and will use that.
109+ If there is not any orderBy clause, it will check if it's an Eloquent model, and will use it's ` primaryKey ` (by default it's 'id').
110+ And if it's not an Eloquent Model, it'll use ` id ` .
111+
112+ ##### Example
113+
114+ ```` php
115+ // Will use Booking's primaryKey
116+ Bookings::cursorPaginate(10);
117+ ````
118+
119+ ```` php
120+ // Will use hardcoded 'id'
121+ DB::table('bookings')->cursorPaginate(10);
122+ ````
123+
124+ ```` php
125+ // Will use 'created_by'
126+ Bookings::orderBy('created_by', 'asc')
127+ ->cursorPaginate(10);
128+ ````
129+
130+ #### Customizing Identifier
131+
132+ Just define the ` identifier ` option
133+
134+ ```` php
135+ // Will use _id, ignoring everything else.
136+ Bookings::cursorPaginate(10, ['*'], [
137+ 'identifier' => '_id'
138+ ]);
139+ ````
140+
141+ ### Date cursors
142+
143+ By default, the identifier is the model's primaryKey (or ` id ` in case it's not an Eloquent model) so there are no duplicates,
144+ but you can adjust this by passing a custom ` identifier ` option. In case that specified identifier is casted to date or datetime
145+ on Eloquent's ` $casts ` property, the paginator will convert it into ** unix timestamp** for you.
146+
147+ You can also specify it manually by adding a ` [ 'date_identifier' => true ] ` option.
148+
149+ ##### Example
150+
151+ Using Eloquent (make sure Booking Model has ` protected $casts = ['datetime' => 'datetime']; ` )
152+
153+ ```` php
154+ // It will autodetect 'datetime' as identifier,
155+ // and will detect it's casted to datetime.
156+ Bookings::orderBy('datetime', 'asc')
157+ ->cursorPaginate(10);
158+ ````
159+
160+ Using Query
161+ ```` php
162+ // It will autodetect 'datetime' as identifier,
163+ // but since there is no model, you'll have to
164+ // specify the 'date_identifier' option to `true`
165+ DB::table('bookings')
166+ ->orderBy('datetime', 'asc')
167+ ->cursorPaginate(10, ['*'], [
168+ 'date_identifier' => true
169+ ]);
170+ ````
171+
98172### Inherits Laravel's Pagination
99173
100174You should know that CursorPaginator inherits from Laravel's AbstractPaginator, so methods like
@@ -125,7 +199,7 @@ Calling `api/v1` will output:
125199 "next_page_url" : " api/v1?next_cursor=3" ,
126200 "prev_page_url" : " api/v1?previous_cursor=1" ,
127201 "data" : [
128- {},
202+ {}
129203 ]
130204}
131205````
@@ -138,7 +212,7 @@ into `links` and `meta`.
138212```` json
139213{
140214 "data" :[
141- {},
215+ {}
142216 ],
143217 "links" : {
144218 "first" : null ,
@@ -151,7 +225,7 @@ into `links` and `meta`.
151225 "previous_cursor" : " 1" ,
152226 "next_cursor" : " 3" ,
153227 "per_page" : 3
154- },
228+ }
155229}
156230````
157231
@@ -193,8 +267,9 @@ To configure that, set `$perPage = [15, 5]`. That way it'll fetch 15 when you do
193267```` php
194268new CursorPaginator(array|collection $items, array|int $perPage, array options = [
195269 // Attribute used for choosing the cursor. Used primaryKey on Eloquent Models as default.
196- 'identifier' => 'id',
197- 'path' => request()->path(),
270+ 'identifier' => 'id',
271+ 'date_identifier' => false,
272+ 'path' => request()->path(),
198273]);
199274````
200275
0 commit comments