@@ -143,23 +143,24 @@ public function __construct(
143
143
* We put `limit` into a new variable to get one more row
144
144
* to understand if it has more pages or not
145
145
*
146
- * @param Model $model
146
+ * @param Builder $builder
147
147
*
148
148
* @return Collection|array
149
149
*/
150
- public function getQueryData ($ model )
150
+ public function getQueryData ($ builder )
151
151
{
152
- $ query = $ model ;
152
+ $ full_identifier_name = $ this ->getFullIdentifierName ($ builder );
153
+ $ query = $ builder ;
153
154
$ limit = $ this ->perPage + 1 ;
154
155
$ this ->has_more_pages = false ;
155
156
if ($ this ->cursor ->getNextCursor ()) {
156
157
// If Cursor Points To Next
157
158
$ query ->take ($ limit )
158
- ->where ($ this -> cursor_identifier_column , $ this ->identifier_sort_inverted ? '< ' : '> ' , $ this ->cursor ->getNextCursor ());
159
+ ->where ($ full_identifier_name , $ this ->identifier_sort_inverted ? '< ' : '> ' , $ this ->cursor ->getNextCursor ());
159
160
} elseif ($ this ->cursor ->getPrevCursor ()) {
160
161
// If Cursor Points To Prev
161
162
$ this ->cursor ->setDirection ('prev ' );
162
- $ sub_query = $ query ->where ($ this -> cursor_identifier_column , $ this ->identifier_sort_inverted ? '> ' : '< ' , $ this ->cursor ->getPrevCursor ())
163
+ $ sub_query = $ query ->where ($ full_identifier_name , $ this ->identifier_sort_inverted ? '> ' : '< ' , $ this ->cursor ->getPrevCursor ())
163
164
->take ($ limit );
164
165
$ full_sub_query = QueryBuilderHelper::exportSqlQuery ($ sub_query );
165
166
$ sub_query ->orderBy ($ this ->cursor_identifier_column , $ this ->identifier_sort_inverted ? 'asc ' : 'desc ' );
@@ -173,8 +174,8 @@ public function getQueryData($model)
173
174
->orderBy ($ this ->cursor_identifier_column , $ this ->identifier_sort_inverted ? 'desc ' : 'asc ' );
174
175
if ($ this ->cursor ->getPrevCursor ()) {
175
176
// Converts Collection to Eloquent Collection
176
- $ data = $ model ->hydrate ($ query ->get ($ this ->columns )->toArray ());
177
- $ data = $ this ->applyModelEagerLoads ($ data , $ model );
177
+ $ data = $ builder ->hydrate ($ query ->get ($ this ->columns )->toArray ());
178
+ $ data = $ this ->applyModelEagerLoads ($ data , $ builder );
178
179
} else {
179
180
$ data = $ query ->get ($ this ->columns );
180
181
}
@@ -217,6 +218,21 @@ public function resolveCurrentCursor()
217
218
return $ cursor ;
218
219
}
219
220
221
+ /**
222
+ * Returns full identifier name. `table_Name`.`identifier_column`
223
+ * For example instead of `id` it will return `users`.`id`
224
+ *
225
+ * @param Builder $builder
226
+ *
227
+ * @return string
228
+ */
229
+ public function getFullIdentifierName ($ builder )
230
+ {
231
+ $ table_name = $ builder ->getModel ()->getTable ();
232
+
233
+ return $ table_name . '. ' .$ this ->cursor_identifier_column ;
234
+ }
235
+
220
236
/**
221
237
* Applies model eagerLoads to the new collection
222
238
* @param Collection $data
@@ -301,6 +317,11 @@ public function prevPageUrl()
301
317
*/
302
318
public function preparePrevCursor ()
303
319
{
320
+ if (!$ this ->items ->count ()) {
321
+ $ this ->prev_cursor = null ;
322
+
323
+ return ;
324
+ }
304
325
$ this ->prev_cursor = $ this ->items ->first ()->{$ this ->getIdentifier ()};
305
326
if ($ this ->prev_cursor instanceof DateTime) {
306
327
$ this ->prev_cursor = $ this ->prev_cursor ->format ('Y-m-d H:i:s ' );
@@ -317,6 +338,11 @@ public function preparePrevCursor()
317
338
*/
318
339
public function prepareNextCursor ()
319
340
{
341
+ if (!$ this ->items ->count ()) {
342
+ $ this ->next_cursor = null ;
343
+
344
+ return ;
345
+ }
320
346
$ this ->next_cursor = $ this ->items ->last ()->{$ this ->getIdentifier ()};
321
347
if ($ this ->next_cursor instanceof DateTime) {
322
348
$ this ->next_cursor = $ this ->next_cursor ->format ('Y-m-d H:i:s ' );
0 commit comments