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.
In order to make the select works I need to explicitly use
limit
in options
and getSearchResultsUsing
is there any better way to do it? and why there is no limit in relationship
method?9 Replies
->limit(100)
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
methodBut why are you using getSearchResults using? when you can use:
And FYI:
should be:
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.
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.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 ':)'
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?
i would accept a PR that adds the option limit to the query in
options()
but ONLY when $component->isSearchable()
is trueOk I will work on it
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...