© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•9mo ago•
3 replies
Matthew

defaultSort() with an enum

Using
defaultsort()
defaultsort()
on an enum column sorts as per the enum value...but the user would be expecting it to sort on the human readable value....

is there a way of overcoming this ?
Solution
Ok, here is a solution, feels a bit hacky, if anyone has a better one....feel free to tell.

In the enum, add a reverse label case method:

    public static function sortCaseSql(string $column) : string
    {
        $cases = collect(self::cases())
            ->map(fn (self $case) => "WHEN '{$case->value}' THEN '" . addslashes($case->getLabel()) . "'"
            )
            ->implode(' ');

        return "CASE $column $cases END";
    } 
    public static function sortCaseSql(string $column) : string
    {
        $cases = collect(self::cases())
            ->map(fn (self $case) => "WHEN '{$case->value}' THEN '" . addslashes($case->getLabel()) . "'"
            )
            ->implode(' ');

        return "CASE $column $cases END";
    } 


In the resource, add a virtual column:

   public static function getEloquentQuery() : Builder
    {
        return parent::getEloquentQuery()
            ->select('*')
            ->selectRaw(enumKbGroup::sortCaseSql('kb_group') . ' as kb_group_label');
    }
   public static function getEloquentQuery() : Builder
    {
        return parent::getEloquentQuery()
            ->select('*')
            ->selectRaw(enumKbGroup::sortCaseSql('kb_group') . ' as kb_group_label');
    }


In the table, just use the virtual column as you would anyway:

         TextColumn::make('kb_group_label')
                ->label('Category')
                ->badge()
                ->sortable(),
         TextColumn::make('kb_group_label')
                ->label('Category')
                ->badge()
                ->sortable(),


and same in the table method:

->defaultSort('kb_group_label', 'asc')
->defaultSort('kb_group_label', 'asc')
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

table ->defaultSort()
FilamentFFilament / ❓┊help
12mo ago
how add defaultSort('') with relationship
FilamentFFilament / ❓┊help
3y ago
translating an ENUM ???
FilamentFFilament / ❓┊help
3y ago
Using an enum with a toggle
FilamentFFilament / ❓┊help
2y ago