From f719440912b18cd2ffeaf050fa8511285f7209cf Mon Sep 17 00:00:00 2001 From: Stefan Schuster Date: Thu, 16 May 2024 13:30:44 +0200 Subject: [PATCH 1/2] Fixing Query expressions as orderBy clauses --- src/FastPaginate.php | 2 ++ tests/Integration/BuilderTest.php | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/FastPaginate.php b/src/FastPaginate.php index 179cd81..0a7948b 100644 --- a/src/FastPaginate.php +++ b/src/FastPaginate.php @@ -125,6 +125,8 @@ public static function getInnerSelectColumns($builder) // is totally reasonable. We'll look for both // quoted and unquoted, as a kindness. // See https://github.com/hammerstonedev/fast-paginate/pull/57 + $column = $column instanceof Expression ? $column->getValue($base->grammar) : $column; + return [ $column, $base->grammar->wrap($column), diff --git a/tests/Integration/BuilderTest.php b/tests/Integration/BuilderTest.php index d90730d..553c293 100644 --- a/tests/Integration/BuilderTest.php +++ b/tests/Integration/BuilderTest.php @@ -235,6 +235,19 @@ public function unquoted_selects_are_preserved_if_used_in_order_by() ); } + /** @test */ + public function using_expressions_for_order_work() + { + $queries = $this->withQueriesLogged(function () use (&$results) { + $results = User::query()->selectRaw('(select 1) as computed_column')->orderBy(User::query()->select('name')->orderBy('name')->limit(1)->getQuery())->fastPaginate(); + }); + + $this->assertEquals( + 'select `users`.`id` from `users` order by (select `name` from `users` order by `name` asc limit 1) asc limit 15 offset 0', + $queries[1]['query'] + ); + } + /** @test */ public function havings_defer() { From d364b2125fd015f14d85667bf987bb9cc56c96e8 Mon Sep 17 00:00:00 2001 From: Aaron Francis Date: Thu, 16 May 2024 10:43:10 -0500 Subject: [PATCH 2/2] Update BuilderTest.php --- tests/Integration/BuilderTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Integration/BuilderTest.php b/tests/Integration/BuilderTest.php index 553c293..df87dc4 100644 --- a/tests/Integration/BuilderTest.php +++ b/tests/Integration/BuilderTest.php @@ -239,7 +239,9 @@ public function unquoted_selects_are_preserved_if_used_in_order_by() public function using_expressions_for_order_work() { $queries = $this->withQueriesLogged(function () use (&$results) { - $results = User::query()->selectRaw('(select 1) as computed_column')->orderBy(User::query()->select('name')->orderBy('name')->limit(1)->getQuery())->fastPaginate(); + $results = User::query()->selectRaw('(select 1) as computed_column')->orderBy( + User::query()->select('name')->orderBy('name')->limit(1)->getQuery() + )->fastPaginate(); }); $this->assertEquals(