Select Field Relationship have no limit

I have over 1M record for projects table. when I use the code below it will get all the projects without any limit for it which making the website not responding.
Forms\Components\Select::make('project_id')
->searchable()
->relationship('project', 'title')
Forms\Components\Select::make('project_id')
->searchable()
->relationship('project', 'title')
In order to make the select works I need to explicitly use limit in options and getSearchResultsUsing
Forms\Components\Select::make('project_id')
->searchable()
->options(Project::limit(10)->get()->pluck('title', 'id'))
->getSearchResultsUsing(function (?string $search) {
return Project::where('title', 'like', "%{$search}%")->limit(10)->get()->pluck('title', 'id');
})
Forms\Components\Select::make('project_id')
->searchable()
->options(Project::limit(10)->get()->pluck('title', 'id'))
->getSearchResultsUsing(function (?string $search) {
return Project::where('title', 'like', "%{$search}%")->limit(10)->get()->pluck('title', 'id');
})
is there any better way to do it? and why there is no limit in relationship method?
9 Replies
toeknee
toeknee6d ago
->limit(100)
Forms\Components\Select::make('project_id')
->searchable()
->limit(50)
->relationship('project', 'title')
Forms\Components\Select::make('project_id')
->searchable()
->limit(50)
->relationship('project', 'title')
Hasan Tahseen
Hasan TahseenOP6d ago
there is no limit method in Select field ":)" there is one called optionsLimit which will not make any effect because it working on the getSearchResultsUsing method and not the options method
toeknee
toeknee5d ago
But why are you using getSearchResults using? when you can use:
Forms\Components\Select::make('project_id')
->searchable()
->optionsLimit(10)
->relationship('project', 'title')
Forms\Components\Select::make('project_id')
->searchable()
->optionsLimit(10)
->relationship('project', 'title')
And FYI:
->options(Project::limit(10)->get()->pluck('title', 'id'))
->options(Project::limit(10)->get()->pluck('title', 'id'))
should be:
->options(fn() => Project::limit(10)->get()->pluck('title', 'id'))
->options(fn() => Project::limit(10)->get()->pluck('title', 'id'))
Hasan Tahseen
Hasan TahseenOP5d ago
Forms\Components\Select::make('project_id')
->searchable()
->optionsLimit(10)
->relationship('project', 'title')
Forms\Components\Select::make('project_id')
->searchable()
->optionsLimit(10)
->relationship('project', 'title')
first. this code will result (timeout exceed error) for relations with big data. please see the implementation of relationship method especially the part of $this->options which will not use (optionsLimit) in it core which result to load all the data.
->options(Project::limit(10)->get()->pluck('title', 'id'))
->options(Project::limit(10)->get()->pluck('title', 'id'))
secondly. above code is better approach since if I use the (fn ()) approach will make the callback method in options a computed method which means the select field will fire a livewire request each time I open and close the select field (which is wrong in my example since I will only display static data that will not relay on any other field and will load when the page is loaded). and the using of getSearchResultsUsing is for searching the records. I will only display 10 or 50 records in the options (and even if I used the fn () approach it will not making it searchable by database also with searchable will not making it search on database). So the only solution here is to specify the limit in the options and the getSearchResultsUsing method So that all things works as expected. please read this docs https://filamentphp.com/docs/3.x/forms/fields/select#returning-custom-search-results.
Hasan Tahseen
Hasan TahseenOP5d ago
this is the code in the ->relationship method. as you can see the implementation of $this->options have nothing related to ->optionsLimit method which means the limits will not applied on the options method and only will applied on the getSearchResultsUsing. So on the first load of the page all the data will be loaded if it 1M then 1M will loaded in the options method ':)'
No description
toeknee
toeknee5d ago
So the fn() approach is recommend to stop excessive DB calls with livewire loops, you can cache the query if you like but putting it in without does cause more queries.. If you are using optionsLimit that should suffice, I would need to further test it myself as not too familiar using that, @Dan Harrin can you advise on the optionsLimit for relationship selects?
Dan Harrin
Dan Harrin5d ago
i would accept a PR that adds the option limit to the query in options() but ONLY when $component->isSearchable() is true
Hasan Tahseen
Hasan TahseenOP5d ago
Ok I will work on it
Hasan Tahseen
Hasan TahseenOP5d ago
GitHub
Adding limit when searchable is true for Select Form Field by ht3aa...
Description This PR add the applity to apply limit on the $this->options when ->relationship with searchable is applied on Select form field Visual changes No visual changes Functiona...

Did you find this page helpful?