diff --git a/packages/tables/src/Columns/Summarizers/Summarizer.php b/packages/tables/src/Columns/Summarizers/Summarizer.php index 90adbbefa06..f0edee099d8 100644 --- a/packages/tables/src/Columns/Summarizers/Summarizer.php +++ b/packages/tables/src/Columns/Summarizers/Summarizer.php @@ -103,6 +103,17 @@ function (EloquentBuilder $relatedQuery) use ($baseQuery, $query): EloquentBuild return $relatedQuery; }, ); + } elseif (str($attribute)->startsWith('pivot.')) { + // https://github.com/filamentphp/filament/issues/12501 + + $pivotAttribute = (string) str($attribute) + ->after('pivot.') + ->prepend('pivot_'); + + $isPivotAttributeSelected = collect($query->getQuery()->getColumns()) + ->contains(fn (string $column): bool => str($column)->endsWith(" as {$pivotAttribute}")); + + $attribute = $isPivotAttributeSelected ? $pivotAttribute : $attribute; } $asName = (string) str($query->getModel()->getTable())->afterLast('.');