Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit d3d2507

Browse files
Merge pull request #4 from aloware/bugfix/ambiguous-identifier
Bugfix | Ambiguous Identifier
2 parents 454c184 + 4d5aede commit d3d2507

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed
File renamed without changes.

src/CursorPaginator.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,24 @@ public function __construct(
143143
* We put `limit` into a new variable to get one more row
144144
* to understand if it has more pages or not
145145
*
146-
* @param Model $model
146+
* @param Builder $builder
147147
*
148148
* @return Collection|array
149149
*/
150-
public function getQueryData($model)
150+
public function getQueryData($builder)
151151
{
152-
$query = $model;
152+
$full_identifier_name = $this->getFullIdentifierName($builder);
153+
$query = $builder;
153154
$limit = $this->perPage + 1;
154155
$this->has_more_pages = false;
155156
if ($this->cursor->getNextCursor()) {
156157
// If Cursor Points To Next
157158
$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());
159160
} elseif ($this->cursor->getPrevCursor()) {
160161
// If Cursor Points To Prev
161162
$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())
163164
->take($limit);
164165
$full_sub_query = QueryBuilderHelper::exportSqlQuery($sub_query);
165166
$sub_query->orderBy($this->cursor_identifier_column, $this->identifier_sort_inverted ? 'asc' : 'desc');
@@ -173,8 +174,8 @@ public function getQueryData($model)
173174
->orderBy($this->cursor_identifier_column, $this->identifier_sort_inverted ? 'desc' : 'asc');
174175
if ($this->cursor->getPrevCursor()) {
175176
// 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);
178179
} else {
179180
$data = $query->get($this->columns);
180181
}
@@ -217,6 +218,21 @@ public function resolveCurrentCursor()
217218
return $cursor;
218219
}
219220

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+
220236
/**
221237
* Applies model eagerLoads to the new collection
222238
* @param Collection $data
@@ -301,6 +317,11 @@ public function prevPageUrl()
301317
*/
302318
public function preparePrevCursor()
303319
{
320+
if (!$this->items->count()) {
321+
$this->prev_cursor = null;
322+
323+
return;
324+
}
304325
$this->prev_cursor = $this->items->first()->{$this->getIdentifier()};
305326
if ($this->prev_cursor instanceof DateTime) {
306327
$this->prev_cursor = $this->prev_cursor->format('Y-m-d H:i:s');
@@ -317,6 +338,11 @@ public function preparePrevCursor()
317338
*/
318339
public function prepareNextCursor()
319340
{
341+
if (!$this->items->count()) {
342+
$this->next_cursor = null;
343+
344+
return;
345+
}
320346
$this->next_cursor = $this->items->last()->{$this->getIdentifier()};
321347
if ($this->next_cursor instanceof DateTime) {
322348
$this->next_cursor = $this->next_cursor->format('Y-m-d H:i:s');

0 commit comments

Comments
 (0)