#Sort table by random order

14 messages · Page 1 of 1 (latest)

golden axle
#

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?

prisma knoll
#

I think what you need to do, is to add a sorting column to your songs table, that you can use to:

  1. Sort the records by in the table
  2. Use a custom table header action to "randomly" shuffle the sort integers of your song records

Then when you trigger your "Randomize" action, the sort would be shuffled, and your song records would appear to be shuffled as well as a result.

#

This is basically what the filament reorder functionality does, but one by one.

golden axle
#

wouldnt that be expensive?

prisma knoll
#

Are you showing thousands of songs at once? You could just shuffle the songs shown in the current view. I don't know how expensive it would be to shuffle all songs in the database, might be worth trying out, though

golden axle
#

obv im not showing all at once, why would i only shuffle the songs in the current page? i want to shuffle them all

prisma knoll
prisma knoll
golden axle
#

oh god

#

yeah

#

idk why i was using ->query

#

changing it to ->baseQuery did the trick

#

thanks @prisma knoll !