F
Filamentβ€’16mo ago
Dimas Angga

getOptionLabelUsing is not working for custom option label result in Select

i cannot make it work using getOptionLabelUsing to make custom option label
Solution:
Here you go. Try this. You'll need to adapt to your columns: ```php Forms\Components\Select::make('user') ->searchable()...
Jump to solution
12 Replies
Kenneth Sese
Kenneth Seseβ€’16mo ago
What's not working? And please be sure copy and paste code and not screenshots
Dimas Angga
Dimas AnggaOPβ€’16mo ago
@archilex im sorry, very newly here, i will elaborate more i want to customize the label option like "NamaPerusahaan - ContactPerson" but always ended up with only "NamaPerusahaan" (screenshot below) below is my code
Select::make('custId')->label('Customer')
->searchable()
->getSearchResultsUsing(fn (string $search): array => Customer::where('NamaPerusahaan', 'like', "%{$search}%")->limit(50)->pluck('NamaPerusahaan', 'id')->toArray())
->getOptionLabelUsing(fn ($value): ?string => Customer::find($value)->NamaPerusahaan . '-' . Customer::find($value)->ContactPerson)
Select::make('custId')->label('Customer')
->searchable()
->getSearchResultsUsing(fn (string $search): array => Customer::where('NamaPerusahaan', 'like', "%{$search}%")->limit(50)->pluck('NamaPerusahaan', 'id')->toArray())
->getOptionLabelUsing(fn ($value): ?string => Customer::find($value)->NamaPerusahaan . '-' . Customer::find($value)->ContactPerson)
Solution
Kenneth Sese
Kenneth Seseβ€’16mo ago
Here you go. Try this. You'll need to adapt to your columns:
Forms\Components\Select::make('user')
->searchable()
->getSearchResultsUsing(fn (string $search): array =>
User::where('name', 'like', "%{$search}%")
->limit(50)
->get()
->mapWithKeys(fn ($user) =>
[$user->id => $user->name . ' - ' . $user->email]
)
->toArray()
)
->getOptionLabelUsing(function ($value): ?string {
$user = User::find($value);
return $user->name . ' - ' . $user->email;
}),

Forms\Components\Select::make('user')
->searchable()
->getSearchResultsUsing(fn (string $search): array =>
User::where('name', 'like', "%{$search}%")
->limit(50)
->get()
->mapWithKeys(fn ($user) =>
[$user->id => $user->name . ' - ' . $user->email]
)
->toArray()
)
->getOptionLabelUsing(function ($value): ?string {
$user = User::find($value);
return $user->name . ' - ' . $user->email;
}),

Kenneth Sese
Kenneth Seseβ€’16mo ago
Basically getSearchResultsUsing() and getOptionLabelUsing() need to match Also you need to set Customer::find() in a variable because if not it you'll be doing duplicate queries to the DB So if you want the labels to have both NamaPerusahaan and ContactPerson then you need to have that as well in the search results
Dimas Angga
Dimas AnggaOPβ€’16mo ago
i see, i understand now, is this documented in official filament v3 ?
Kenneth Sese
Kenneth Seseβ€’16mo ago
Did that work for you?
Dimas Angga
Dimas AnggaOPβ€’16mo ago
yes! it is works for me, big love for u ❀️ #nogay hahaha
Kenneth Sese
Kenneth Seseβ€’16mo ago
It doesn't say that explicitly in the docs..., but the example in the docs has the two methods matching. I might make a PR to make it more clear. Great!
Dimas Angga
Dimas AnggaOPβ€’16mo ago
because i surfing along the documentation and have no result big thanks for you!
Dimas Angga
Dimas AnggaOPβ€’16mo ago
@archilex one more question if you have more time for this discussion πŸ™‚ i want to show all "NamaPerusahaan" first when first load, but again, i want to customize the label option, if i directly use
->options(Customer::all()->pluck('NamaPerusahaan', 'id'))
->options(Customer::all()->pluck('NamaPerusahaan', 'id'))
it will only show "NamaPerusahaan" only in the label, but when doing search, it works perfectly for the label option, do you know something about this ? or we can use the method like inside
->getSearchResultsUsing
->getSearchResultsUsing
this ?
Kenneth Sese
Kenneth Seseβ€’16mo ago
Same thing. Use that match there instead of the pluck
Dimas Angga
Dimas AnggaOPβ€’16mo ago
ok, i will try around, thanks! great! it is worked after modified to
->options(fn (): array =>
Customer::all()
->mapWithKeys(fn ($customer) =>
[$customer->id => $customer->NamaPerusahaan . ' - ' . $customer->ContactPerson]
)
->toArray()
)
->options(fn (): array =>
Customer::all()
->mapWithKeys(fn ($customer) =>
[$customer->id => $customer->NamaPerusahaan . ' - ' . $customer->ContactPerson]
)
->toArray()
)
Want results from more Discord servers?
Add your server