filament icon indicating copy to clipboard operation
filament copied to clipboard

Allow orderby (and other mysql stuff) in filters

Open Edsardio opened this issue 2 years ago • 0 comments

I might be completely incorrect, but I think this would allow users to use filters for ordering their tables. I do understand this might not be wanted so feel free to decline the PR if that's the case.

Especially nifty if you're using the Layout::AboveContent, which with a little bit of css might give a filter the look of being an order dropdown instead of a filter.

Currently doing:

Filter::make('Sorteervolgorde')
    ->form([
        Select::make('Sorteervolgorde')
            ->disablePlaceholderSelection()
            ->options([
                'newestFirst'       => 'Nieuwste eerst',
                'oldestFirst'       => 'Oudste eerst',
            ]),
    ])
    ->query(function (Builder $query, array $data): Builder {
        return $query
            ->when($data['Sorteervolgorde'] === 'newestFirst',
                fn(Builder $query) => $query->orderBy('id', 'DESC'))
            ->when($data['Sorteervolgorde'] === 'oldestFirst',
                fn(Builder $query) => $query->orderBy('id'));
    }),

Is not possible as the current implementation only allows where queries. This change would allow this example to work.

Edsardio avatar Aug 10 '22 15:08 Edsardio