Skip to content

Commit 6a1fb26

Browse files
Merge pull request #65 from sschuster/main
Fixing Query expressions as orderBy clauses
2 parents a4bf5cf + d364b21 commit 6a1fb26

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/FastPaginate.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public static function getInnerSelectColumns($builder)
125125
// is totally reasonable. We'll look for both
126126
// quoted and unquoted, as a kindness.
127127
// See https://github.com/hammerstonedev/fast-paginate/pull/57
128+
$column = $column instanceof Expression ? $column->getValue($base->grammar) : $column;
129+
128130
return [
129131
$column,
130132
$base->grammar->wrap($column),

tests/Integration/BuilderTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,21 @@ public function unquoted_selects_are_preserved_if_used_in_order_by()
235235
);
236236
}
237237

238+
/** @test */
239+
public function using_expressions_for_order_work()
240+
{
241+
$queries = $this->withQueriesLogged(function () use (&$results) {
242+
$results = User::query()->selectRaw('(select 1) as computed_column')->orderBy(
243+
User::query()->select('name')->orderBy('name')->limit(1)->getQuery()
244+
)->fastPaginate();
245+
});
246+
247+
$this->assertEquals(
248+
'select `users`.`id` from `users` order by (select `name` from `users` order by `name` asc limit 1) asc limit 15 offset 0',
249+
$queries[1]['query']
250+
);
251+
}
252+
238253
/** @test */
239254
public function havings_defer()
240255
{

0 commit comments

Comments
 (0)