F
Filamentβ€’6d ago
Tuim

Select with live() and searchable()

Hi all, I am trying to make a double select (depending on relation) that is searchable. First of all I made a new component with artisan make:form-field to create a re-usable component. This component has the following schema, pretty much following the documentation
$this->schema([
Select::make('category')
->options(Category::query()->whereNull('parent_id')->get()->pluck('label', 'id'))
->live(),

Select::make('sub_category')
->searchable()
->options(function (Get $get) {
$valueToSearchFor = $get('category');
if (blank($valueToSearchFor)) {
return [];
}

return Category::query()
->where('parent_id', $get('_stabu_selector'))
->get()->pluck('label', 'id');
})
]);
$this->schema([
Select::make('category')
->options(Category::query()->whereNull('parent_id')->get()->pluck('label', 'id'))
->live(),

Select::make('sub_category')
->searchable()
->options(function (Get $get) {
$valueToSearchFor = $get('category');
if (blank($valueToSearchFor)) {
return [];
}

return Category::query()
->where('parent_id', $get('_stabu_selector'))
->get()->pluck('label', 'id');
})
]);
However if I add ->searchable() to the first select it appears to stop working. The 2nd dropdown isn't populated according to the query (just stays empty) There is no error message really.
3 Replies
Tuim
TuimOPβ€’6d ago
I have a feeling that it might be because the category is not actually a field in my model's data properties?
LeandroFerreira
LeandroFerreiraβ€’6d ago
$get('_stabu_selector')) shouldn't be $valueToSearchFor ?
Tuim
TuimOPβ€’6d ago
Yeah it is, I was just trying to make it more clear by removing domain code πŸ˜‰ Updated my code above Either way, $valueToSearchFor is always null so I think that is because it isnt actually a property on my model category isnt actually a field in this case its just a "helper" field to make searching for the final category in sub_category easier Hmm, the issue might be circumvented. I need that selector in a repeater and then it works
Want results from more Discord servers?
Add your server