Options Format

I want to format the way my options are shown , I used getOptionLabelFromRecordUsing() but it is only working in single select but it doesnot work with multiple(). What can I use in multiple select to format my options label ?
7 Replies
LeandroFerreira
->getOptionLabelsUsing ?
namrata
namrataOP2y ago
Nope did not work.
Forms\Components\Select::make('products')
->options(Product::where('active', true)->publicDisplay()->pluck('sku', 'id'))
->getOptionLabelUsing(fn (Model $record) => "{$record->name} | {$record->sku}")
->multiple()
->reactive()
->required()
->afterStateUpdated(function ($livewire, $state) {
$livewire->dispatch('productAdded', $state);
}),
Forms\Components\Select::make('products')
->options(Product::where('active', true)->publicDisplay()->pluck('sku', 'id'))
->getOptionLabelUsing(fn (Model $record) => "{$record->name} | {$record->sku}")
->multiple()
->reactive()
->required()
->afterStateUpdated(function ($livewire, $state) {
$livewire->dispatch('productAdded', $state);
}),
namrata
namrataOP2y ago
This is my code and this is the respone
No description
Kenneth Sese
Kenneth Sese2y ago
Is there a reason you can't use ->relationship()? getOptionLabelFromRecordUsing() is working with me with relationship() relationship also has a query parameter so you can use your scope.
namrata
namrataOP2y ago
The field is json saved llike this [1407,14283,6594,1007] so I cannot use relationship in this
Kenneth Sese
Kenneth Sese2y ago
@namrata_1999 Try this:
Forms\Components\Select::make('products')
->options(Product::where('active', true)
->publicDisplay()
->get()
->mapWithKeys(fn ($product) =>
[$product->id => "{$product->name} | {$product->sku}"]
)
->toArray()
)
->getOptionLabelsUsing(fn (array $values): array =>
Product::whereIn('id', $values)
->get()
->mapWithKeys(fn ($product) =>
[$product->id => "{$product->name} | {$product->sku}"]
)
->toArray()
)
->multiple()
->reactive()
->required()
->afterStateUpdated(function ($livewire, $state) {
$livewire->dispatch('productAdded', $state);
}),
Forms\Components\Select::make('products')
->options(Product::where('active', true)
->publicDisplay()
->get()
->mapWithKeys(fn ($product) =>
[$product->id => "{$product->name} | {$product->sku}"]
)
->toArray()
)
->getOptionLabelsUsing(fn (array $values): array =>
Product::whereIn('id', $values)
->get()
->mapWithKeys(fn ($product) =>
[$product->id => "{$product->name} | {$product->sku}"]
)
->toArray()
)
->multiple()
->reactive()
->required()
->afterStateUpdated(function ($livewire, $state) {
$livewire->dispatch('productAdded', $state);
}),
namrata
namrataOP2y ago
Thank you but this is messes up the search results. It formatted the options but the results of serach are not matching my search parameter

Did you find this page helpful?