F
Filament12mo ago
namrata

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:
Instead of pluck, use map with keys, eg: ```php ->options(function () { return Model::all()->mapWithKeys(function ($model) { return [$model->id => $model->name . $model->id ...];...
Jump to solution
19 Replies
Hussain4real
Hussain4real12mo ago
What are you trying to achieve
namrata
namrataOP12mo ago
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
namrata
namrataOP12mo ago
but it is only givin the sku
No description
Hussain4real
Hussain4real12mo ago
There's 's' to be added to the label it should be plural getOptionLabelsUsing()
namrata
namrataOP12mo ago
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
namrata
namrataOP12mo ago
No description
Hussain4real
Hussain4real12mo ago
Remove the first options Use ->getSearchResultsUsing()
namrata
namrataOP12mo ago
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
Hussain4real
Hussain4real12mo ago
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?
namrata
namrataOP12mo ago
did not work its a field name, that saves multiple product ids in json
Hussain4real
Hussain4real12mo ago
Try pasting the form function of the resource in readable format(triple quotes) It seems you didn't append searchable() function
namrata
namrataOP12mo ago
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())
->searchable()
->multiple()
->reactive()
->required()
->afterStateUpdated(function ($livewire, $state) {
$livewire->dispatch('productAdded', $state);
})->columnSpanFull(),
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())
->searchable()
->multiple()
->reactive()
->required()
->afterStateUpdated(function ($livewire, $state) {
$livewire->dispatch('productAdded', $state);
})->columnSpanFull(),
Solution
Jordy
Jordy12mo ago
Instead of pluck, use map with keys, eg:
->options(function () {
return Model::all()->mapWithKeys(function ($model) {
return [$model->id => $model->name . $model->id ...];
});
})
->options(function () {
return Model::all()->mapWithKeys(function ($model) {
return [$model->id => $model->name . $model->id ...];
});
})
Jordy
Jordy12mo ago
discord formatting strikes again (fixed, ish)
Hussain4real
Hussain4real12mo ago
Follow this filament order:
Select::make('technologies')
->multiple()
->searchable()
->getSearchResultsUsing(fn (string $search): array => Technology::where('name', 'like', "%{$search}%")->limit(50)->pluck('name', 'id')->toArray())
->getOptionLabelsUsing(fn (array $values): array => Technology::whereIn('id', $values)->pluck('name', 'id')->toArray()),
Select::make('technologies')
->multiple()
->searchable()
->getSearchResultsUsing(fn (string $search): array => Technology::where('name', 'like', "%{$search}%")->limit(50)->pluck('name', 'id')->toArray())
->getOptionLabelsUsing(fn (array $values): array => Technology::whereIn('id', $values)->pluck('name', 'id')->toArray()),
Jordy
Jordy12mo ago
you dont need 2 queries for this.. can prob rewrite this to an arrow function as this syntax kinda sucks w the double return
namrata
namrataOP12mo ago
this worked !! Thanks a lot !!
Jordy
Jordy12mo ago
no problem ^^
namrata
namrataOP12mo ago
Thank you for helping !!
Want results from more Discord servers?
Add your server