filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
17.65k stars 2.76k forks source link

Model custom attribute and column sort #951

Closed aliloubm closed 2 years ago

aliloubm commented 2 years ago

Package

filament/filament

Package Version

v2.6.3

Laravel Version

v8

Livewire Version

v2

PHP Version

PHP 8

Bug description

If i use custom attribute in my model :

Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fullname' in 'order clause' (SQL: select * from `bulletin` order by `fullname` asc limit 10 offset 0) (View: /xxx/vendor/filament/tables/resources/views/index.blade.php)

Steps to reproduce

I have a custom attribute in a model

    public function getFullnameAttribute()
    {
        return "$this->name - ".($this->datadate)->formatLocalized('%B %Y');
    }

and in ressource page

public static function table(Table $table): Table
    {
        return $table
            ->columns([
                //
                Tables\Columns\TextColumn::make('fullname')->sortable()->label('Titre'),
                Tables\Columns\TextColumn::make('datadate')->date('m/Y')->sortable()->label('Date'),
                Tables\Columns\BooleanColumn::make('published')->label('Publié')
                    ->trueIcon('heroicon-o-check-circle')
                    ->falseIcon('heroicon-o-x-circle')
                ])
            ->filters([
                //
            ]);
    }

Relevant log output

No response

danharrin commented 2 years ago

Hey, you need to pass the column names to sort to the sortable() method: sortable(['name']).

aliloubm commented 2 years ago

thx,

sortable(['name','datadate']) work perfectly as intended