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

Commit cefd4d8

Browse files
Merge pull request #9 from aloware/fix/multiple-cursor-conditions
Fix Multiple Cursor Conditions While We Are in a Loop
2 parents 9924e47 + db78c25 commit cefd4d8

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/CursorPaginator.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Pagination\Paginator as PaginatorContract;
99
use Illuminate\Contracts\Support\Arrayable;
1010
use Illuminate\Contracts\Support\Jsonable;
11+
use Illuminate\Database\Eloquent\Builder;
1112
use Illuminate\Http\Request;
1213
use Illuminate\Pagination\AbstractPaginator;
1314
use Illuminate\Support\Carbon;
@@ -164,8 +165,11 @@ public function getQueryData($builder)
164165

165166
if ($this->cursor->getNextCursor()) {
166167
// If Cursor Points To Next
167-
$query->take($limit)
168-
->where($full_identifier_name, $this->identifier_sort_inverted ? '<' : '>', $this->cursor->getNextCursor());
168+
$next_cursor = $this->cursor->getNextCursor();
169+
170+
$this->overrideCursorCondition($query, $full_identifier_name, $next_cursor, $this->identifier_sort_inverted);
171+
$query->take($limit);
172+
169173
} elseif ($this->cursor->getPrevCursor()) {
170174
// If Cursor Points To Prev
171175
$this->cursor->setDirection('prev');
@@ -629,4 +633,39 @@ protected function getCursorLink($cursor, $is_pointing_next = true)
629633

630634
return $link . '?cursor=' . $cursor;
631635
}
636+
637+
/**
638+
* Overrides Cursor Condition with the new Cursor Value
639+
*
640+
* @param Builder $builder
641+
* @param string $full_identifier_name
642+
* @param int $next_cursor_value
643+
* @param bool $identifier_sort_inverted
644+
*
645+
* @return void
646+
*/
647+
protected function overrideCursorCondition( $builder , string $full_identifier_name, int $next_cursor_value, bool $identifier_sort_inverted): void
648+
{
649+
if($builder instanceof Builder) {
650+
$query = $builder->getQuery();
651+
} else {
652+
$query = $builder;
653+
}
654+
655+
$bindings = $query->getBindings();
656+
657+
if($query->wheres[count($query->wheres)-1]['column'] == $full_identifier_name) {
658+
$query->wheres[count($query->wheres)-1]['value'] = $next_cursor_value;
659+
$bindings[count($bindings)-1] = $next_cursor_value;
660+
} else {
661+
$query->where($full_identifier_name, $identifier_sort_inverted ? '<' : '>', $next_cursor_value);
662+
$bindings[] = $next_cursor_value;
663+
}
664+
665+
$query->setBindings( $bindings );
666+
667+
if ($builder instanceof Builder) {
668+
$builder->setQuery($query)->withoutGlobalScopes();
669+
}
670+
}
632671
}

0 commit comments

Comments
 (0)