Skip to content

Commit

Permalink
Merge pull request #57 from hebinet/sort-with-custom-select
Browse files Browse the repository at this point in the history
Custom Selects with an additional order by or query constraint ar not working
  • Loading branch information
aarondfrancis authored May 15, 2024
2 parents 273cda1 + b4da72b commit 8016238
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/FastPaginate.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,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) {
Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,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()
{
Expand Down

0 comments on commit 8016238

Please sign in to comment.