FilamentF
Filament15mo ago
ericmp

Sort table by random order

In my songs table, i want to add a filter to let the user choose to order the records randomly:

Tables\Filters\Filter::make('in_random_order')->query(fn (Builder $query): Builder => $query->inRandomOrder())


but if i check it or not, the results are the same. what could be happening? how can i make it to work?
Solution
Okay, I got curious so I added a 2000 song table to a fresh filament installation and did some experimenting. got it to work by using baseQuery instead of query (https://filamentphp.com/docs/3.x/tables/filters/getting-started#modifying-the-base-query) :
->filters([
  Filter::make('Shuffle')
    ->toggle() 
    ->baseQuery(fn (Builder $query) => $query->inRandomOrder())
    ->label('Shuffle Songs'),
])
Was this page helpful?