Skip to content

Commit

Permalink
Reverse aliases for column ordering with cursor pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Jul 12, 2021
1 parent ecf4c96 commit 06a4b80
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
31 changes: 30 additions & 1 deletion src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Container\Container;
use Illuminate\Database\MultipleRecordsFoundException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Pagination\CursorPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
Expand Down Expand Up @@ -305,7 +306,9 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =
$builder->where(function (self $builder) use ($addCursorConditions, $cursor, $orders, $i) {
['column' => $column, 'direction' => $direction] = $orders[$i];

$builder->where($column, $direction === 'asc' ? '>' : '<', $cursor->parameter($column));
$original = $this->reverseColumnAliasingForCursorPagination($this, $column);

$builder->where($original, $direction === 'asc' ? '>' : '<', $cursor->parameter($column));

if ($i < $orders->count() - 1) {
$builder->orWhere(function (self $builder) use ($addCursorConditions, $column, $i) {
Expand All @@ -327,6 +330,32 @@ protected function paginateUsingCursor($perPage, $columns = ['*'], $cursorName =
]);
}

/**
* Reverse any aliases columns for column ordering.
*
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder
* @param string $parameter
* @return string
*/
protected function reverseColumnAliasingForCursorPagination($builder, string $parameter)
{
$columns = $builder instanceof Builder ? $builder->getQuery()->columns : $builder->columns;

if (! is_null($columns)) {
foreach ($columns as $column) {
if (stripos($column, ' as ') !== false) {
[$original, $alias] = explode(' as ', $column);

if ($parameter === $alias) {
return $original;
}
}
}
}

return $parameter;
}

/**
* Create a new length-aware paginator instance.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Pagination/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Cursor implements Arrayable
*
* @var array
*/
protected $parameters;
public $parameters;

/**
* Determine whether the cursor points to the next or previous set of items.
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4271,7 +4271,7 @@ protected function getMySqlBuilderWithProcessor()
}

/**
* @return m\MockInterface|Builder
* @return \Mockery\MockInterface|\Illuminate\Database\Query\Builder
*/
protected function getMockQueryBuilder()
{
Expand Down

0 comments on commit 06a4b80

Please sign in to comment.