-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
is any way use default filter when the resource open #848
Comments
Add a global scope with where(year, date('Y')) in the indexQuery. And remove it in the filter. |
@dkulyk
and use the filter
i think the $query is $query->where('Year','2018')->where('Year','2017').... how can i set the default Year and user can change it by filter?
Now how can i get the $request->filter name and params......... |
This is example of global scope of a query. You need to implement In the resource: public static function indexQuery(NovaRequest $request, $query)
{
$query->withGlobalScope(DefaultYearScope::class, new DefaultYearScope());
} And in the filter: public function apply(Request $request, $query, $value)
{
return $query->withoutGlobalScope(DefaultYearScope::class)->where(...);
} |
HI @dkulyk and thx a lot for your suggestion! I have a problem implementing it however. I seem to be unable to use:
in my filter. It works on the index, and it is applied correctly but for some reason disabling it does not work. Any ideas what could be the cause for that? |
There is no:
There should Then it works. Not sure why ->withGlobalScope does not work in this particular scenario. |
In your filter, specify the defaults by using the |
Hi @davidhemphill by now you mean in the latest release right? |
Ok I changed the way I update to private repo. Can you tell me how to implement it please? It does not seem to work for me, my filter looks like this:
|
the value on the right side is the key when it comes to filters: return [
'Admin' => 'admin', // <-- this is the key
'User' => 'user', // <-- this is the key
] And so you would specify |
Hi @davidhemphill ! Thank you for your suggestion. It works but not as expected. It seems like it will work well on a table with a small number of fields. If I get it down to 3 then on every refresh default filter kicks in. I, however, I increase the number of fields in the table it seems like the default filter query gets overwritten my the letter index query. When I looked at my network tab I could see all the queries being executed. Please see attached where I have default filter set up and it only kicks in sometimes depending on how long the query takes to execute. This is the only way I could figure this out. It seems like index query is being executed after filter query even though it should not be the case. It seems like disabling just two of the BelongsTo:: does the trick but it still does not guarantee 100% correct behaviour.
|
Just for future readers, a global scope is not necessary anymore, this is now (v2.?) possible with adding a default to a filter:
|
thanks it works |
the model has Year filed ,i use filter to display
Has any way use the default filter just like 2018 when the resource list?
The text was updated successfully, but these errors were encountered: