F
Filament2mo ago
Eskie

getOptionLabelUsing doesnt trigger

Select::make('revenue_district_office_id')
->searchable()
->getSearchResultsUsing(fn (string $search): array => RevenueDistrictOffice::query()
->where('code', 'like', "%{$search}%")
->orWhere('location', 'like', "%{$search}%")
->limit(10)
->pluck('code', 'id')
->all())
->required()
->getOptionLabelUsing(function (string $value) {
$record = RevenueDistrictOffice::find($value);
if ($record === null) {
return '';
}

return $record->code.' - '.$record->location;
}),
Select::make('revenue_district_office_id')
->searchable()
->getSearchResultsUsing(fn (string $search): array => RevenueDistrictOffice::query()
->where('code', 'like', "%{$search}%")
->orWhere('location', 'like', "%{$search}%")
->limit(10)
->pluck('code', 'id')
->all())
->required()
->getOptionLabelUsing(function (string $value) {
$record = RevenueDistrictOffice::find($value);
if ($record === null) {
return '';
}

return $record->code.' - '.$record->location;
}),
No description
9 Replies
Dennis Koch
Dennis Koch2mo ago
Just pass the formatted value via getSearchResultsUsing()
Eskie
EskieOP2mo ago
Select::make('revenue_district_office_id') ->label('Revenue District Office') ->searchable() ->getSearchResultsUsing(fn (string $search): array => RevenueDistrictOffice::query() ->where('code', 'like', "%{$search}%") ->orWhere('location', 'like', "%{$search}%") ->limit(10) ->pluck(DB::raw("CONCAT(code, ' - ', location)"), 'id') ->all()) ->required() ->preload(), why is it preload isn't working?
toeknee
toeknee2mo ago
please don't tag @Eskie as per #✅┊rules . preload doesn't work with searchable since it's searching on typing
Eskie
EskieOP2mo ago
Sorry.. thank you! Preload is working if i am not using getsearchresultusing even if the select is searchable..
toeknee
toeknee2mo ago
Interesting, I assumed preload only loads (options or relationships) not search results snce you are not yet searching..
Eskie
EskieOP2mo ago
Select::make('creative_domains')
->options(CreativeDomain::pluck('name', 'id'))
->required()
->optionsLimit(2)
->preload()
->searchable()
->columnSpanFull(),
Select::make('creative_domains')
->options(CreativeDomain::pluck('name', 'id'))
->required()
->optionsLimit(2)
->preload()
->searchable()
->columnSpanFull(),
No description
Eskie
EskieOP2mo ago
this is one of my selects but if i will use getSearchResultsUsing.. preload doesn't work
toeknee
toeknee2mo ago
What if you do:
Select::make('creative_domains')
->options(fn() => CreativeDomain::pluck('name', 'id'))
->required()
->optionsLimit(2)
->preload()
->searchable(['code','location'])
->columnSpanFull(),
Select::make('creative_domains')
->options(fn() => CreativeDomain::pluck('name', 'id'))
->required()
->optionsLimit(2)
->preload()
->searchable(['code','location'])
->columnSpanFull(),
I'd also be tempted to relationship the options too.
TDDeveloper
TDDeveloper2mo ago
I guess you are trying to format the option labels? Maybe try ->getOptionLabelFromRecordUsing(fn ($record) => "{$record->code} - {$record->location}")

Did you find this page helpful?