Searchable Not Working When Preload is Set For Select Field With relationship

I have this code
Forms\Components\Select::make('product_id')
->label('Product')
->required()
->preload()
->searchable()
->relationship('product', 'title')
->prefixIcon(IconEnum::PRODUCT->value)
->placeholder('Select a product')
->helperText('Product this promo code applies to'),
Forms\Components\Select::make('product_id')
->label('Product')
->required()
->preload()
->searchable()
->relationship('product', 'title')
->prefixIcon(IconEnum::PRODUCT->value)
->placeholder('Select a product')
->helperText('Product this promo code applies to'),
when the preload is set the searchable will not work because of this code in filament select field
public function hasDynamicSearchResults(): bool
{
if ($this->hasRelationship() && empty($this->searchColumns)) {
return ! $this->isPreloaded();
}

return $this->getSearchResultsUsing instanceof Closure;
}
public function hasDynamicSearchResults(): bool
{
if ($this->hasRelationship() && empty($this->searchColumns)) {
return ! $this->isPreloaded();
}

return $this->getSearchResultsUsing instanceof Closure;
}
above code will block the code of $this->getSearchResultsUsing which defined in the relationship method. My question is why is that? In my use cases I want to preload the data at first with some limit and allow user to search for the rest of the data.
27 Replies
Hasan Tahseen
Hasan TahseenOP4mo ago
Any suggestions?
Jonathan Christiani
if you set ->optionLimits(10) -> is the data exist? just to make sure if the data loaded correctly
Hasan Tahseen
Hasan TahseenOP4mo ago
no need for optionLimits. it already take the default limit which is 50 when you are using relationship with searchable. and yeah the data will load but the search will not works since the hasDynamicSearchResults will return false because the preload is set which makes the search feature by database disabled. if you remove the preload then the search will works as expected but you will lose the ability to see option loaded when open the select field
Hasan Tahseen
Hasan TahseenOP4mo ago
Forms\Components\Select::make('product_id')
->label('Product')
->required()
->optionsLimit(10)
->searchable()
->relationship('product', 'title')
->prefixIcon(IconEnum::PRODUCT->value)
->placeholder('Select a product')
->helperText('Product this promo code applies to'),
Forms\Components\Select::make('product_id')
->label('Product')
->required()
->optionsLimit(10)
->searchable()
->relationship('product', 'title')
->prefixIcon(IconEnum::PRODUCT->value)
->placeholder('Select a product')
->helperText('Product this promo code applies to'),
No description
Jonathan Christiani
i know why it doesnt work try this
Forms\Components\Select::make('product_id')
->label('Product')
->relationship('classroom','name')
->searchable()
->optionsLimit(5)
->preload()
->prefixIcon(IconEnum::PRODUCT->value)
->placeholder('Select a product')
->helperText('Product this promo code applies to'),
Forms\Components\Select::make('product_id')
->label('Product')
->relationship('classroom','name')
->searchable()
->optionsLimit(5)
->preload()
->prefixIcon(IconEnum::PRODUCT->value)
->placeholder('Select a product')
->helperText('Product this promo code applies to'),
change the order
Hasan Tahseen
Hasan TahseenOP4mo ago
I try it but still the same problem.
Jonathan Christiani
oky, let set other option, cuz i dont think thats because of this method
hasDynamicSearchResults
hasDynamicSearchResults
ive done it before, my code look like this
Select::make('classroom_id')
->relationship('classroom','name')
->label(__('form.classroom'))
->searchable()
->optionsLimit(5)
->preload()
Select::make('classroom_id')
->relationship('classroom','name')
->label(__('form.classroom'))
->searchable()
->optionsLimit(5)
->preload()
and it works did u changed the relationship right? not use classroom
Hasan Tahseen
Hasan TahseenOP4mo ago
Of course bro. Please read the functionality of hasDynamicSearchResults its says that if the field has relation (which means if relationship is set) and the search column is set also (which means you specifiy the column in the relationship) then check if preload is set return false otherwise return true. So the dynamic search will not work if its preloaded. (Dynamic search by database not a normal search in front end)
Jonathan Christiani
so if you delete ->preload() and the ->searchable() it will show the options? just want to make sure the relationship is works and the data is exist in the options, cuz sometimes i did sth wrong with the data or relationship, cuz its same code, i try ur code but with my own data, it works, the preload works and i can still search the data
Hasan Tahseen
Hasan TahseenOP4mo ago
What do you mean by search is it normal search or database search? When you search does word "searching" appears?
Jonathan Christiani
this is wht i mean i can still search the data even tho i use preload
No description
Jonathan Christiani
so i search matthew, and it will show all the matthew data is that wht u mean by search? or did i missunderstnd u
Hasan Tahseen
Hasan TahseenOP4mo ago
Its a normal search bro. Try to search a data that is not displayed at first open of the select feild. Like if you display the firs 10 records try search for a records outside these 10
Jonathan Christiani
yeah it still works, i set the preload 5 and it shows 5 data, then i search for the record that not displayed in first 5 data that shows in the first
Hasan Tahseen
Hasan TahseenOP4mo ago
Can you please record a video when you search?. Just to see how the search is working.
Jonathan Christiani
im using ur same script but diffrent relationship
Hasan Tahseen
Hasan TahseenOP4mo ago
Yeah its a normal search bro. Fill your data with records more than 100 record Then search for the last record It will not get it Or remove preload And search again You will see different kind of search and each time you search it will take time to get the data Which means (dynamic search is enabled)
Jonathan Christiani
i've 50 data already, whts the diffrent with 100 data? i have 50 classroom
Hasan Tahseen
Hasan TahseenOP4mo ago
The defaul option limit is 50
Jonathan Christiani
ok so ill try add one more so it will51
Hasan Tahseen
Hasan TahseenOP4mo ago
Can you check this apporoch Delete the preload
Jonathan Christiani
still works
Hasan Tahseen
Hasan TahseenOP4mo ago
And you will see different kind of search I will show you the difference between the dynamic and normal search
Jonathan Christiani
i've also try this, i have 51 data and try to search the last one and it works i know the diffrent if we dont use preload it will takes some time, but if we add the preload it will search not that long well my point is, with the same code as u put it still works in my project, idk wht exacly made urs not work as mine, but yeah it works on me hahaha
Hasan Tahseen
Hasan TahseenOP4mo ago
Ok according to this issue https://github.com/filamentphp/filament/issues/16423 Its solved in the latest versions
GitHub
Dynamic Search Not Working When preload is set for a relationship S...
Package filament/filament Package Version v3.3.18 Laravel Version v12.x Livewire Version v3.x PHP Version 8.2 Problem description I have this code Select::make('product_id') ->preload() ...

Did you find this page helpful?