Skip to content

Commit

Permalink
Merge pull request #5608 from Laravel-Backpack/do-not-add-order-by-pr…
Browse files Browse the repository at this point in the history
…imary-key-when-grouping

do not add order by primary key if there are group by in the query
  • Loading branch information
pxpm authored Aug 14, 2024
2 parents 39f5133 + 0dd141b commit 3394165
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/app/Library/CrudPanel/Traits/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,14 @@ public function applyDatatableOrder()
$orderBy = $this->query->toBase()->orders;
$table = $this->model->getTable();
$key = $this->model->getKeyName();
$groupBy = $this->query->toBase()->groups;

$hasOrderByPrimaryKey = collect($orderBy)->some(function ($item) use ($key, $table) {
return (isset($item['column']) && $item['column'] === $key)
|| (isset($item['sql']) && str_contains($item['sql'], "$table.$key"));
});

if (! $hasOrderByPrimaryKey) {
if (! $hasOrderByPrimaryKey && empty($groupBy)) {
$this->orderByWithPrefix($key, 'DESC');
}
}
Expand Down

0 comments on commit 3394165

Please sign in to comment.