Is it possible to sort options in a Select form field?

I have a Select field pulling in options from a relationship. Trouble is, they are sorted alphabetically, but I have a custom sort order on the model using a sort_order column. I don't see anything in the docs that indicates that this is possible, but figured I'd ask before I venture down the road of a custom field (would rather not). Here's my code example:
Select::make('noteServices')
->multiple()
->preload()
->relationship(
name: 'services',
titleAttribute: 'name'
),
Select::make('noteServices')
->multiple()
->preload()
->relationship(
name: 'services',
titleAttribute: 'name'
),
1 Reply
noahlocke
noahlockeOP7mo ago
SOLVED: my brain just needed to fart a few times:
Select::make('noteServices')
->multiple()
->preload(true)
->relationship(
name: 'services',
titleAttribute: 'name',
modifyQueryUsing:
fn (Builder $query) => $query->orderBy('sort_order', 'asc')
),
Select::make('noteServices')
->multiple()
->preload(true)
->relationship(
name: 'services',
titleAttribute: 'name',
modifyQueryUsing:
fn (Builder $query) => $query->orderBy('sort_order', 'asc')
),
Want results from more Discord servers?
Add your server