Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Selects with an additional order by or query constraint ar not working #57

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/FastPaginate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down