Dependent select searchable

I can't seem to make this work. The second selection doesn't seem to populate. If I use the options ()instead of getSearchResultsUsing() and getOptionLabelUsing() Without searchable(), the second selection populates but doesn't clear it. But I want both of the selects is searchable.
Select::make('drawer_id')
->searchable()

// ->options(Drawer::query()->pluck('name', 'id'))
// ->afterStateUpdated(fn (Set $set) => $set('folder_id', []))

->getSearchResultsUsing(fn (string $search): array => Drawer::query()->pluck('name', 'id')->toArray())
->getOptionLabelUsing(fn ($value): ?string => Drawer::find($value)?->name)

->reactive(),

Select::make('folder_id')
>searchable()
->getSearchResultsUsing(fn (Get $get): array => Folder::where('drawer_id', $get('drawer_id'))->limit(50)->pluck('name', 'id')->toArray())
->getOptionLabelUsing(fn ($value): ?string => Drawer::find($value)?->name),
Select::make('drawer_id')
->searchable()

// ->options(Drawer::query()->pluck('name', 'id'))
// ->afterStateUpdated(fn (Set $set) => $set('folder_id', []))

->getSearchResultsUsing(fn (string $search): array => Drawer::query()->pluck('name', 'id')->toArray())
->getOptionLabelUsing(fn ($value): ?string => Drawer::find($value)?->name)

->reactive(),

Select::make('folder_id')
>searchable()
->getSearchResultsUsing(fn (Get $get): array => Folder::where('drawer_id', $get('drawer_id'))->limit(50)->pluck('name', 'id')->toArray())
->getOptionLabelUsing(fn ($value): ?string => Drawer::find($value)?->name),
14 Replies
Dennis Koch
Dennis Koch16mo ago
The second selection doesn't seem to populate
Did you search for anything? Otherwise there are no values. You need ->preload() if you want values before searching.
If I use the options ()instead of getSearchResultsUsing() and getOptionLabelUsing() Without searchable(), the second selection populates but doesn't clear it.
What do you mean by: Doesn't clear it? Why should it be cleared? If you want to clear the second select after selecting a value in the first one, you need to add something like this on the first select:
->afterStateUpdated(fn (Set $set) => $set('folder_id', null))
->afterStateUpdated(fn (Set $set) => $set('folder_id', null))
PS: On the second select you query Folder but try to get an option for Drawer.
->getSearchResultsUsing(
fn (Get $get): array => Folder::where('drawer_id', $get('drawer_id'))
->limit(50)
->pluck('name', 'id')->toArray()
)
->getOptionLabelUsing(fn ($value): ?string => Drawer::find($value)?->name),
->getSearchResultsUsing(
fn (Get $get): array => Folder::where('drawer_id', $get('drawer_id'))
->limit(50)
->pluck('name', 'id')->toArray()
)
->getOptionLabelUsing(fn ($value): ?string => Drawer::find($value)?->name),
neverender24
neverender24OP16mo ago
If I comment searchable() the $get() will get the drawer_id, but if uncomment it, it will get a null value. That's why I can't query in the second select when using searchable in both select.
Select::make('drawer_id')
// ->searchable()

->options(Drawer::query()->pluck('name', 'id'))
->afterStateUpdated(fn (Set $set) => $set('folder_id', null))
->live()
->preload(),

Select::make('folder_id')
->searchable()
->getSearchResultsUsing(fn (Get $get): array => dd($get('drawer_id'))) //this will get null if drawer searchable is uncomment
->getOptionLabelUsing(fn ($value): ?string => Folder::find($value)?->name),
Select::make('drawer_id')
// ->searchable()

->options(Drawer::query()->pluck('name', 'id'))
->afterStateUpdated(fn (Set $set) => $set('folder_id', null))
->live()
->preload(),

Select::make('folder_id')
->searchable()
->getSearchResultsUsing(fn (Get $get): array => dd($get('drawer_id'))) //this will get null if drawer searchable is uncomment
->getOptionLabelUsing(fn ($value): ?string => Folder::find($value)?->name),
Dennis Koch
Dennis Koch16mo ago
It's null after you selected a value in the first select?
neverender24
neverender24OP16mo ago
yes after I selected the first select which is the drawer
Dennis Koch
Dennis Koch16mo ago
Hmm. Weird one. Can you make sure you are on the latest versions of Livewire and Filament?
neverender24
neverender24OP16mo ago
Here's a video comparison with searchable() commented and not
neverender24
neverender24OP16mo ago
I attached video also for reference
Dennis Koch
Dennis Koch16mo ago
Can you check fn ($livewire) => dd($livewire->data) on getSearchResultsUsing()
neverender24
neverender24OP16mo ago
it returns empty array
Dennis Koch
Dennis Koch16mo ago
Is this ever fired on the first select ->afterStateUpdated(fn (Set $set) => dd('state updated')?
neverender24
neverender24OP16mo ago
afterStateUpdated() doesn't fire the dd(), but if I comment out searchable() it fires.
Dennis Koch
Dennis Koch16mo ago
Okay, so this seems like a Filament or Livewire bug. Can you report this on GitHub please? @Dan Harrin Are there any known issues with searchable Select fields not working live()?
neverender24
neverender24OP16mo ago
Okay I will, thanks
Dan Harrin
Dan Harrin16mo ago
not as far as I remember
Want results from more Discord servers?
Add your server