F
Filament5mo ago
ericmp

Modify label in select options

Forms\Components\Select::make('song_id')
->relationship(name: 'song', titleAttribute: 'name')
Forms\Components\Select::make('song_id')
->relationship(name: 'song', titleAttribute: 'name')
id like to change the title attribute, i currently have it like this (song name):
Levels
Levels
but id like to show the artist too:
Avicii - Levels
Avicii - Levels
but idk how to do it hmmm
Solution:
final code: ```php Forms\Components\Select::make('song_id') ->searchable() ->relationship(name: 'song', titleAttribute: 'name')...
Jump to solution
15 Replies
ericmp
ericmp5mo ago
if i make it searchable doesnt work i need to make it searchable
ericmp
ericmp5mo ago
example when it works:
Forms\Components\Select::make('song_id')
->native(false)
->relationship(
name: 'song',
)
->getOptionLabelFromRecordUsing(function (Song $record) {
$artistName = $record->artists()->first()?->name ?? '';

return "{$artistName} - {$record->name}";
})
,
Forms\Components\Select::make('song_id')
->native(false)
->relationship(
name: 'song',
)
->getOptionLabelFromRecordUsing(function (Song $record) {
$artistName = $record->artists()->first()?->name ?? '';

return "{$artistName} - {$record->name}";
})
,
No description
ericmp
ericmp5mo ago
example when it doesnt:
Forms\Components\Select::make('song_id')
->native(false)
->searchable()
->relationship(
name: 'song',
)
->getOptionLabelFromRecordUsing(function (Song $record) {
$artistName = $record->artists()->first()?->name ?? '';

return "{$artistName} - {$record->name}";
})
Forms\Components\Select::make('song_id')
->native(false)
->searchable()
->relationship(
name: 'song',
)
->getOptionLabelFromRecordUsing(function (Song $record) {
$artistName = $record->artists()->first()?->name ?? '';

return "{$artistName} - {$record->name}";
})
No description
ericmp
ericmp5mo ago
how to make it searchable but it also shows the full song name with the artist? also tried: ->searchable(['song.name', 'artists.name']) doesnt work
Tim van Heugten
Tim van Heugten5mo ago
Looks like it should work. Does searchable work for that second example if you remove the custom option labels? Should the search also return hits for the artist name? If for whatever reason it doesn’t work you could also try without a relation and just use getSearchResultsUsing.
ericmp
ericmp5mo ago
"Does searchable work for that second example if you remove the custom option labels?" yes the idea for now is search by song name
Tim van Heugten
Tim van Heugten5mo ago
The example in the docs has searchable after the relationship and getOptionLabel… Did you try that (it shouldn’t matter and I don’t have access to the code to check right now but who knows).
ericmp
ericmp5mo ago
yeah already tried it same result seems im not missing anything but idk
Tim van Heugten
Tim van Heugten5mo ago
use Filament\Forms\Components\Select;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

Select::make('author_id')
->relationship(
name: 'author',
modifyQueryUsing: fn (Builder $query) => $query->orderBy('first_name')->orderBy('last_name'),
)
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->first_name} {$record->last_name}")
->searchable(['first_name', 'last_name'])
use Filament\Forms\Components\Select;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

Select::make('author_id')
->relationship(
name: 'author',
modifyQueryUsing: fn (Builder $query) => $query->orderBy('first_name')->orderBy('last_name'),
)
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->first_name} {$record->last_name}")
->searchable(['first_name', 'last_name'])
So if you try this example from the docs and leave out the artist for now, does that work? Just return the song title
ericmp
ericmp5mo ago
i think i have it before: ->relationship(name: 'song') now: ->relationship(name: 'song', titleAttribute: 'name') let me double check
Solution
ericmp
ericmp5mo ago
final code:
Forms\Components\Select::make('song_id')
->searchable()
->relationship(name: 'song', titleAttribute: 'name')
->required()
->getOptionLabelFromRecordUsing(function (Song $record) {
$artistName = $record->artists()->first()?->name ?? '';

return "{$artistName} - {$record->name}";
})
,
Forms\Components\Select::make('song_id')
->searchable()
->relationship(name: 'song', titleAttribute: 'name')
->required()
->getOptionLabelFromRecordUsing(function (Song $record) {
$artistName = $record->artists()->first()?->name ?? '';

return "{$artistName} - {$record->name}";
})
,
like this works fine
Tim van Heugten
Tim van Heugten5mo ago
That would make sense because it won’t know what column to search without it It has a default I think, but that might be ‘name’. Or actually ‘title’ Glad it works now
ericmp
ericmp5mo ago
yeah i see, yeah it may (: thanks for ur time bro avicii was named tim too xd
ericmp
ericmp5mo ago
just if u wanna have a look - https://discord.com/channels/883083792112300104/1199422071776755853 🙌 🙌 🙌 😛
Want results from more Discord servers?
Add your server
More Posts