FilamentF
Filament13mo ago
bionary

Can I sort table on non-column data

I know tables sort at the SQL level. That's why sorting like this does not work out of the box in Filament:
 TextColumn::make('days-past-due')->label('Past due')
                    ->color('danger')
                    ->visible(fn ($livewire) => $livewire->activePresetView == 'to-do-now')
                    ->sortable()
                    ->getStateUsing(function($record){
                        $followUpDate = $record->client_events->sortBy('follow_up_on')->firstWhere('is_complete', false)?->follow_up_on;
                        if($followUpDate){
                            $daysPastDue = ceil(now()->diffInDays($followUpDate));//will be negative so round up
                            return "$daysPastDue days";
                        }
                    }),


What I am doing above is presenting the oldest date (in days) to the user via that little internal query there.

Does anybody have any idea on how to sort based on some information that is not stored in the table? I'm out of ideas.
Was this page helpful?