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()


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 '),
        ];
    }

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)

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 ?
Was this page helpful?