Skip to content

Commit 38c2126

Browse files
[9.x] Fix aliasing with cursor pagination (#45188)
* Fix aliasing with cursor pagination * add testCursorPaginateWithDynamicColumnWithCastInSelectRaw * Update BuildsQueries.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent f06af0c commit 38c2126

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/Illuminate/Database/Concerns/BuildsQueries.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,10 @@ protected function getOriginalColumnNameForCursorPagination($builder, string $pa
427427

428428
if (! is_null($columns)) {
429429
foreach ($columns as $column) {
430-
if (($position = stripos($column, ' as ')) !== false) {
431-
$as = substr($column, $position, 4);
430+
if (($position = strripos($column, ' as ')) !== false) {
431+
$original = substr($column, 0, $position);
432432

433-
[$original, $alias] = explode($as, $column);
433+
$alias = substr($column, $position + 4);
434434

435435
if ($parameter === $alias || $builder->getGrammar()->wrap($parameter) === $alias) {
436436
return $original;

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4632,6 +4632,47 @@ public function testCursorPaginateWithDynamicColumnInSelectRaw()
46324632
]), $result);
46334633
}
46344634

4635+
public function testCursorPaginateWithDynamicColumnWithCastInSelectRaw()
4636+
{
4637+
$perPage = 15;
4638+
$cursorName = 'cursor';
4639+
$cursor = new Cursor(['test' => 'bar']);
4640+
$builder = $this->getMockQueryBuilder();
4641+
$builder->from('foobar')->select('*')->selectRaw('(CAST(CONCAT(firstname, \' \', lastname) as VARCHAR)) as test')->orderBy('test');
4642+
$builder->shouldReceive('newQuery')->andReturnUsing(function () use ($builder) {
4643+
return new Builder($builder->connection, $builder->grammar, $builder->processor);
4644+
});
4645+
4646+
$path = 'http://foo.bar?cursor='.$cursor->encode();
4647+
4648+
$results = collect([['test' => 'foo'], ['test' => 'bar']]);
4649+
4650+
$builder->shouldReceive('get')->once()->andReturnUsing(function () use ($builder, $results) {
4651+
$this->assertEquals(
4652+
'select *, (CAST(CONCAT(firstname, \' \', lastname) as VARCHAR)) as test from "foobar" where ((CAST(CONCAT(firstname, \' \', lastname) as VARCHAR)) > ?) order by "test" asc limit 16',
4653+
$builder->toSql());
4654+
$this->assertEquals(['bar'], $builder->bindings['where']);
4655+
4656+
return $results;
4657+
});
4658+
4659+
CursorPaginator::currentCursorResolver(function () use ($cursor) {
4660+
return $cursor;
4661+
});
4662+
4663+
Paginator::currentPathResolver(function () use ($path) {
4664+
return $path;
4665+
});
4666+
4667+
$result = $builder->cursorPaginate();
4668+
4669+
$this->assertEquals(new CursorPaginator($results, $perPage, $cursor, [
4670+
'path' => $path,
4671+
'cursorName' => $cursorName,
4672+
'parameters' => ['test'],
4673+
]), $result);
4674+
}
4675+
46354676
public function testCursorPaginateWithDynamicColumnInSelectSub()
46364677
{
46374678
$perPage = 15;

0 commit comments

Comments
 (0)