Skip to content

Commit 333722b

Browse files
Fix duplicate joins when resolving multiple columns
Check query's existing joins instead of a local array, preventing duplicate table joins when resolveColumnWithJoins is called multiple times (e.g. for both groupBy and aggregate columns). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c4a217b commit 333722b

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/ReportBuilder.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ protected function resolveColumnWithJoins(Builder $query, string $key): string
6161
$parts = explode('.', $key);
6262
$columnName = array_pop($parts);
6363
$currentModel = $query->getModel();
64-
$joined = [];
64+
$existingJoins = collect($query->getQuery()->joins ?? [])
65+
->pluck('table')
66+
->all();
6567

6668
foreach ($parts as $relationName) {
6769
if (! method_exists($currentModel, $relationName)) {
@@ -71,7 +73,7 @@ protected function resolveColumnWithJoins(Builder $query, string $key): string
7173
$relation = $currentModel->{$relationName}();
7274
$relatedTable = $relation->getRelated()->getTable();
7375

74-
if (in_array($relatedTable, $joined, true)) {
76+
if (in_array($relatedTable, $existingJoins, true)) {
7577
$currentModel = $relation->getRelated();
7678

7779
continue;
@@ -93,7 +95,7 @@ protected function resolveColumnWithJoins(Builder $query, string $key): string
9395
);
9496
}
9597

96-
$joined[] = $relatedTable;
98+
$existingJoins[] = $relatedTable;
9799
$currentModel = $relation->getRelated();
98100
}
99101

0 commit comments

Comments
 (0)