F
Filament12mo ago
Wirkhof

Modifying the option label in Select

I would like to modify the option's label in the select box. My working code without any added text looks like this:
->schema([
Select::make('user_id')
->label('Warrior')
->required()
->searchable()
->options(User::all()->pluck('name', 'id')
])
->schema([
Select::make('user_id')
->label('Warrior')
->required()
->searchable()
->options(User::all()->pluck('name', 'id')
])
And this works and the user names are shown based on what you type in the select box. I would like to add a custom text (I will replace it later with a custom generated quote like "I only trust my sword" etc.) at the end of the name. So, instead of what I have now:
Jar Grimsh
Witek Boiar
Zilgert Brimbright
Jar Grimsh
Witek Boiar
Zilgert Brimbright
I would want:
Jar Grimsh - I only trust my sword.
Witek Boiar - War, war, and war!
Zilgert Brimbright - Light over darkness.
Jar Grimsh - I only trust my sword.
Witek Boiar - War, war, and war!
Zilgert Brimbright - Light over darkness.
In the results in the select box as the user is typing stuff in it. How to alter the option output to reflect the added text? Let's say the labels are stored in the table called quotes and they have an id and quote for the quote itself. And it should be random. Or just with a custom static text or html like - my custom quote or <b style='color: green;'> - my custom quote</b> I have tried suggestions my some Discord users like:
->schema([
Select::make('user_id')
->label('Warrior')
->required()
->searchable()
->options(User::all()->pluck('name', 'id')
->getOptionLabelFromRecordUsing(fn(Model $record) => "{$record->name} - my custom text")
])
->schema([
Select::make('user_id')
->label('Warrior')
->required()
->searchable()
->options(User::all()->pluck('name', 'id')
->getOptionLabelFromRecordUsing(fn(Model $record) => "{$record->name} - my custom text")
])
But this is not working and the output is still just the name. The part " - my custom text" is missing.
Solution:
maybe something like this. ```php Select::make('user_id') ->label('Warrior') ->required()...
Jump to solution
7 Replies
LeandroFerreira
LeandroFerreira12mo ago
->options(User::pluck('name', 'id')->map(function ($item) {
return match ($item) {
default => $item,
'Jar Grimsh' => "{$item} - I only trust my sword."
};
}))
->options(User::pluck('name', 'id')->map(function ($item) {
return match ($item) {
default => $item,
'Jar Grimsh' => "{$item} - I only trust my sword."
};
}))
I think an accessor would be better https://laravel.com/docs/10.x/eloquent-mutators#accessors-and-mutators
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Solution
awcodes
awcodes12mo ago
maybe something like this.
Select::make('user_id')
->label('Warrior')
->required()
->searchable()
->options(function(): array {
return User::all()->mapWithKeys(function ($user) {
return [$user->id => $user->name . ' - ' . $user->quote];
})->toArray();
})
Select::make('user_id')
->label('Warrior')
->required()
->searchable()
->options(function(): array {
return User::all()->mapWithKeys(function ($user) {
return [$user->id => $user->name . ' - ' . $user->quote];
})->toArray();
})
BlackShadow
BlackShadow12mo ago
If its a BelongsTo relationship can't you use relationship('author', 'name') in combination with getOptionLabelFromRecordUsing ?
awcodes
awcodes12mo ago
Yes, but I didn’t see a relationship in the code provided. 🙂
BlackShadow
BlackShadow12mo ago
True! But if @Wirkhof didn't know you can also use it that way.
Wirkhof
WirkhofOP12mo ago
Thank you, guys. The mapWithKeys exampl seems to be working.
KeyMe
KeyMe9mo ago
little late to the party, is it possible to append at the beginning of label instead? have u tried that?
Want results from more Discord servers?
Add your server