Skip to content
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

Closed
loveyuxiao opened this issue Oct 17, 2018 · 12 comments
Closed

is any way use default filter when the resource open #848

loveyuxiao opened this issue Oct 17, 2018 · 12 comments

Comments

@loveyuxiao
Copy link

the model has Year filed ,i use filter to display

class demo extends Filter
{
    public function apply(Request $request, $query, $value)
    {
        return $query->where('Year',$value);
    }
    public function options(Request $request)
    {
        return [
            '2018',
            '2017',
            '2016',
            '2015',  
        ];
    }
}

Has any way use the default filter just like 2018 when the resource list?

@dkulyk
Copy link

dkulyk commented Oct 17, 2018

Add a global scope with where(year, date('Y')) in the indexQuery. And remove it in the filter.
This way added condition with the current year by default.

@loveyuxiao
Copy link
Author

@dkulyk
i set global scope in resource indexQuery.

    public static function indexQuery(NovaRequest $request, $query)
    {
        return $query->where('Year',\date('Y'));
    }

and use the filter

class demo extends Filter
{
    public function apply(Request $request, $query, $value)
    {
        return $query->where('Year',$value);
    }
    public function options(Request $request)
    {
        return [
            '2017',
        ];
    }
}

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?
just like this?

    public static function indexQuery(NovaRequest $request, $query)
    {
       // check  $request->fitler ==( class demo extends Filter)???
        if ($request->fitler==demo filter)
        {
            return $query->where('Year',\date('Y'));
        else
       {
            return $query;
        }
    }

Now how can i get the $request->filter name and params.........

@dkulyk
Copy link

dkulyk commented Oct 18, 2018

This is example of global scope of a query. You need to implement Illuminate\Database\Eloquent\Scope for using it.

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(...);
}

@aurawindsurfing
Copy link

HI @dkulyk and thx a lot for your suggestion! I have a problem implementing it however. I seem to be unable to use:

public function apply(Request $request, $query, $value)
    {
        return $query->withoutGlobalScope(UpcomingOnlyScope::class);
    }

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?

@aurawindsurfing
Copy link

There is no:

public static function indexQuery(NovaRequest $request, $query)
{
    $query->withGlobalScope(DefaultYearScope::class, new DefaultYearScope());
}

There should addGlobalScope on the boot method of the model.

Then it works. Not sure why ->withGlobalScope does not work in this particular scenario.

@davidhemphill
Copy link
Contributor

In your filter, specify the defaults by using the default method.

@aurawindsurfing
Copy link

Hi @davidhemphill by now you mean in the latest release right?

@aurawindsurfing
Copy link

aurawindsurfing commented Nov 29, 2018

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:

 public function apply(Request $request, $query, $value)
    {
        return $query->where('date', '>=', $value);
    }

    /**
     * Set the default options for the filter.
     *
     * @return array
     */
    public function default()
    {
        return [
            'Upcoming'
        ];
    }

    /**
     * Get the filter's available options.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function options(Request $request)
    {
        return [
            'Upcoming' => now()->format('Y-m-d h:m:s'),
            'All' => date('1900-01-01 00:00:00'),
        ];
    }

@davidhemphill
Copy link
Contributor

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 admin as the default.

@aurawindsurfing
Copy link

aurawindsurfing commented Nov 30, 2018

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.

BelongsTo::make('Venue')->sortable()->searchable()->rules('required'),
BelongsTo::make('Tutor')->sortable()->searchable()->rules('required'),

kapture 2018-11-30 at 23 15 32

@ndberg
Copy link

ndberg commented Oct 23, 2019

Just for future readers, a global scope is not necessary anymore, this is now (v2.?) possible with adding a default to a filter:

    /**
     * Apply default on resource open 
     *
     * @return array|int|mixed
     */
    public function default()
    {
        return 3;
    }

@ahmadrio
Copy link

There is no:

public static function indexQuery(NovaRequest $request, $query)
{
    $query->withGlobalScope(DefaultYearScope::class, new DefaultYearScope());
}

There should addGlobalScope on the boot method of the model.

Then it works. Not sure why ->withGlobalScope does not work in this particular scenario.

thanks it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants