Custom option label in multiple select
I have tried getOptionLabelUsing and getOptionLabelFromRecordUsing() both but my the options are not as I wanted. I have faced this when the select field is multiple.
getOptionLabelFromRecordUsing() this works in single select option but not in multiple
Solution:Jump to solution
Instead of pluck, use map with keys, eg:
```php
->options(function () {
return Model::all()->mapWithKeys(function ($model) {
return [$model->id => $model->name . $model->id ...];...
19 Replies
What are you trying to achieve
Forms\Components\Select::make('products')
->options(Product::where('active', true)->publicDisplay()->pluck('sku', 'id'))
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->name} | {$record->sku}")
->multiple()
->reactive()
->required()
this is my code
but it is only givin the sku
There's 's' to be added to the label it should be plural
getOptionLabelsUsing()
Forms\Components\Select::make('products')
->options(Product::where('active', true)->publicDisplay()->pluck('sku', 'id'))
->getOptionLabelsUsing(fn (Model $record) => "{$record->name} | {$record->sku}")
->multiple()
->reactive()
->required()
but still the result is the same
Remove the first options
Use
->getSearchResultsUsing()
Forms\Components\Select::make('products')
// ->options(Product::where('active', true)->publicDisplay()->pluck('sku', 'id'))
// ->getOptionLabelsUsing(fn (Model $record) => "{$record->name} | {$record->sku}")
// ->getSearchResultsUsing(fn (Model $record) => "{$record->name} | {$record->sku}")
->getSearchResultsUsing(fn (string $search): array => Product::where('name', 'like', "%{$search}%")
->orWhere('sku', 'like', "%{$search}%")
->limit(50)->pluck('name', 'id')->toArray())
->multiple()
->reactive()
->required()
none of these work
Let the search function comes before the options label function
When pasting your code enclose it in triple quotes for readability
Can you provide the scope of the resource, is products a relationship name or just a field?
did not work
its a field name, that saves multiple product ids in json
Try pasting the form function of the resource in readable format(triple quotes)
It seems you didn't append
searchable()
functionSolution
Instead of pluck, use map with keys, eg:
discord formatting strikes again (fixed, ish)
Follow this filament order:
you dont need 2 queries for this..
can prob rewrite this to an arrow function as this syntax kinda sucks w the double return
this worked !! Thanks a lot !!
no problem ^^
Thank you for helping !!