F
Filament4mo ago
nathan

Form Select - Search Across Relationship

I have 3 resources. - OfficeResource (Model: Office) - OfficeAddressResource (Model: OfficeAddress) - CountryResource (Model: Country) I have a form on the OfficeResource with a select field. This select field searches the OfficeAddress table and allows for the selection of an address. The OfficeAddress model has a hasone relationship with the model Country. Problem: On my OfficeResource form, I want to search by the address, city, and country.common_name in the address select form element. How would I accomplish this? (I would love to add something like ->searchable(['street_address', 'city', 'country.common_name']) But I know that's not possible. Here is the select element on the OfficeResource form:
Select::make('office_physical_address_id')
->label('Physical Address')
->relationship(name: 'physicalAddress', titleAttribute: 'street_address')
->getOptionLabelFromRecordUsing(fn(OfficeAddress $record) => $record->full_address)
->searchable(['street_address', 'city'])
->createOptionForm(OfficeAddressForm::getForm())
->editOptionForm(OfficeAddressForm::getForm())
->helperText('Click the + button to add a new address, or choose from the dropdown.')
->preload(),
Select::make('office_physical_address_id')
->label('Physical Address')
->relationship(name: 'physicalAddress', titleAttribute: 'street_address')
->getOptionLabelFromRecordUsing(fn(OfficeAddress $record) => $record->full_address)
->searchable(['street_address', 'city'])
->createOptionForm(OfficeAddressForm::getForm())
->editOptionForm(OfficeAddressForm::getForm())
->helperText('Click the + button to add a new address, or choose from the dropdown.')
->preload(),
Solution:
Modify the return options?
->getSearchResultsUsing(fn (string $search): array => OfficeAddress::where('street_address', 'like', "%{$search}%")->orWhere('city', 'like', "%{$search}%")->limit(50)->pluck('name', 'id')->toArray())
->getSearchResultsUsing(fn (string $search): array => OfficeAddress::where('street_address', 'like', "%{$search}%")->orWhere('city', 'like', "%{$search}%")->limit(50)->pluck('name', 'id')->toArray())
...
Jump to solution
3 Replies
nathan
nathanOP4mo ago
bump... Anybody have thoughts on this?
Solution
toeknee
toeknee4mo ago
Modify the return options?
->getSearchResultsUsing(fn (string $search): array => OfficeAddress::where('street_address', 'like', "%{$search}%")->orWhere('city', 'like', "%{$search}%")->limit(50)->pluck('name', 'id')->toArray())
->getSearchResultsUsing(fn (string $search): array => OfficeAddress::where('street_address', 'like', "%{$search}%")->orWhere('city', 'like', "%{$search}%")->limit(50)->pluck('name', 'id')->toArray())
nathan
nathanOP4mo ago
Perfect, thank you!
Want results from more Discord servers?
Add your server