Skip to content

Commit 907d2ae

Browse files
Fix group by sorting to respect user-selected sort column
The reorder() call was wiping out any user-selected sort and always ordering by the group column. Now respects sortBy when set to 'aggregate' or 'group_value', allowing users to sort by count/sum/etc. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0cc1d6d commit 907d2ae

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/ReportBuilder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,19 @@ public function rowsQuery()
4343
: "{$baseTable}.{$this->aggregateColumn}";
4444
}
4545

46+
$sortColumn = in_array($this->sortBy, ['aggregate', 'group_value'], true)
47+
? $this->sortBy
48+
: $groupByColumn;
49+
50+
$sortDirection = in_array($this->sortBy, ['aggregate', 'group_value'], true)
51+
? $this->sortDirection
52+
: 'asc';
53+
4654
$query->withoutGlobalScope('order')
4755
->reorder()
4856
->selectRaw("{$groupByColumn} as group_value, {$this->aggregateFunction}({$aggregateColumn}) as aggregate")
4957
->groupBy($groupByColumn)
50-
->orderBy($groupByColumn);
58+
->orderBy($sortColumn, $sortDirection);
5159

5260
return $query;
5361
}

0 commit comments

Comments
 (0)