© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
12 replies
kwatman

Exporting a sorted sum column with ExportAction

Im trying to export a list of users with a sum column.

im my table for users i added this:
   TextColumn::make('rides_sum_distance')
                    ->label('Total distance')
                    ->sum('rides', 'distance')
                    ->default(0)
                    ->formatStateUsing(fn (string $state): string =>  number_format($state, 2, ',', '.') . ' km')
                    ->sortable()
                    ->badge()
   TextColumn::make('rides_sum_distance')
                    ->label('Total distance')
                    ->sum('rides', 'distance')
                    ->default(0)
                    ->formatStateUsing(fn (string $state): string =>  number_format($state, 2, ',', '.') . ' km')
                    ->sortable()
                    ->badge()


The get colums method for the exporter looks like this:

   public static function getColumns(): array
    {
        return [
            ExportColumn::make('id')
                ->label('ID'),
            ExportColumn::make('name'),
            ExportColumn::make('email'),
            ExportColumn::make('created_at'),
            ExportColumn::make('rides_sum_distance')
                ->sum('rides', 'distance')
                ->default(0)
                ->formatStateUsing(fn (string $state): string => number_format($state, 2, ',', '.') . ' km '),
        ];
    }
   public static function getColumns(): array
    {
        return [
            ExportColumn::make('id')
                ->label('ID'),
            ExportColumn::make('name'),
            ExportColumn::make('email'),
            ExportColumn::make('created_at'),
            ExportColumn::make('rides_sum_distance')
                ->sum('rides', 'distance')
                ->default(0)
                ->formatStateUsing(fn (string $state): string => number_format($state, 2, ',', '.') . ' km '),
        ];
    }

this all works. the table shows the sum correctly and i can export it. however when i sort the table on the rides_sum_distance i get the following error for the PrepareCsvExport job:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'rides_sum_distance' in 'order clause' (Connection: mysql, SQL: select distinct * from `users` order by `rides_sum_distance` desc limit 100 offset 0)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'rides_sum_distance' in 'order clause' (Connection: mysql, SQL: select distinct * from `users` order by `rides_sum_distance` desc limit 100 offset 0)

It seems like the moment a sort is added the export looks for the colomn in the database. Is there any way to prevent this ?
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

ExportAction exporting not all rows?
FilamentFFilament / ❓┊help
3mo ago
ExportAction with MInIO
FilamentFFilament / ❓┊help
2y ago
How to Sum relationship column with cast field
FilamentFFilament / ❓┊help
15mo ago
Custom summaries (Sum of other column)
FilamentFFilament / ❓┊help
3y ago