diff --git a/src/FastPaginate.php b/src/FastPaginate.php index 2c73821..758ae90 100644 --- a/src/FastPaginate.php +++ b/src/FastPaginate.php @@ -117,10 +117,16 @@ public static function getInnerSelectColumns($builder) ->pluck('column') ->filter() ->map(function ($column) use ($base) { - // Use the grammar to wrap them, so that our `str_contains` - // (further down) doesn't return any false positives. - return $base->grammar->wrap($column); - }); + // Not everyone quotes their custom selects, which + // is totally reasonable. We'll look for both + // quoted and unquoted, as a kindness. + // See https://github.com/hammerstonedev/fast-paginate/pull/57 + return [ + $column, + $base->grammar->wrap($column) + ]; + }) + ->flatten(1); return collect($base->columns) ->filter(function ($column) use ($orders, $base) { diff --git a/tests/Integration/BuilderTest.php b/tests/Integration/BuilderTest.php index 47de73e..f5e1b7a 100644 --- a/tests/Integration/BuilderTest.php +++ b/tests/Integration/BuilderTest.php @@ -204,6 +204,19 @@ public function selects_are_overwritten() ); } + /** @test */ + public function unquoted_selects_are_preserved_if_used_in_order_by() + { + $queries = $this->withQueriesLogged(function () use (&$results) { + $results = User::query()->selectRaw('(select 1) as computed_column')->orderBy('computed_column')->fastPaginate(); + }); + + $this->assertEquals( + 'select `users`.`id`, (select 1) as computed_column from `users` order by `computed_column` asc limit 15 offset 0', + $queries[1]['query'] + ); + } + /** @test */ public function havings_defer() {