dkulyk/nova-dependent-filter

This package is abandoned and no longer maintained. The author suggests using the awesome-nova/dependent-filter package instead.

Dependent filters for Laravel Nova

v1.0.0 2019-02-01 10:31 UTC

This package is auto-updated.

Last update: 2022-02-01 13:15:48 UTC


README

Nova Dependent Filter

Latest Version on Github Total Downloads Become a Patron!

  1. Installation
  2. Usage
    1. Declaration
    2. Class declaration
    3. Static dependencies
    4. Dynamic dependencies
    5. Hiding empty filters
    6. Default filter value
    7. Other stuffs
  3. Thanks

Installation

You can install the package in to a Laravel app that uses Nova via composer:

composer require dkulyk/nova-dependent-filter

Usage

Declaration

You can declare filters in you filters method directly:

function filters(Request $request)
{
    return [
        (new DependentFilter('State'))
            ->withOptions([
                'all' => 'All orders',
                'draft' => 'Draft',
                'outstanding' => 'Outstanding',
                'past_due' => 'Past due',
                'paid' => 'Paid',
            ]),
    ];
}

Also you can use DependentFilter::make() instead new DependentFilter().

For queries you need to use callback declaration:

function filters(Request $request)
{
    return [
        DependentFilter::make('Category', 'category_id'))
            ->withOptions(function (Request $request) {
                return Category::pluck('title', 'id');
            }),
    ];
}

Note: In difference with Nova filters filter's value need pass as array key and label as array value.

Class declaration

As is Nova filters you can create filter's class:

class CategoryFilter extends DependentFilter
{
    /**
     * Name of filter.
     *
     * @var string
     */
    public $name = 'Category';
    
    /**
     * Attribute name of filter. Also it is key of filter.
     *
     * @var string
     */
    public $attribute = 'category_id';
    
    public function options(Request $request, array $filters = [])
    {
        return Category::pluck('title', 'id');
    } 
}

Note: The fresh method is identical with the callback for declaring options.

function filters(Request $request)
{
    return [
        CategoryFilter::make(),
    ];
}

Static dependencies

For creating dependent filter you need to specify dependent filters values at which the option will be shown:

function filters(Request $request)
{
    return [
        CategoryFilter::make(),
        
        SubCategory::make('Subcategory', 'subcategory_id')
            ->withOptions(function (Request $request) {
                return SubCategory::all()->map(function ($subcategory) {
                    return [
                        'value' => $subcategory->id,
                        'label' => $subcategory->title.
                        'depends' => [
                            'category_id' => $subcategory->category_id, //Also you can set array of values
                        ],
                    ];
                });
            }),
    ];
}

Note. Instead of an attribute or class name, you must specify the key of the filter.

Dynamic dependencies

For big collection of data you can use dynamic updating of the filter.

function filters(Request $request) 
{
    return [
        StateFilter::make('State', 'state_id'),
        
        DependentFilter::make('City', 'city_id')
            ->dpendentOf('state_id')
            ->withOptions(function (Request $request, $filters) {
                return City::where('state_id', $filters['state_id'])
                    ->pluck('title', 'id');
            }),
    ];
}

In class declaration you also need to set $dpendentOf property:

class CityFilter extends DependentFilter
{
    public $dependentOf = ['state_id'];
    
    function options(Request $request, $filters = [])
    {
        return City::where('state_id', $filters['state_id'])
            ->pluck('title', 'id');
    }
}

If you want to show options only when main filter is selected you can use when for check it:

function options(Request $request, $filters = []) {
    return City::when($filters['state_id'], function ($query, $value) {
        $query->where('state_id', $value)
    })->pluck('title', 'id');
}

Hiding empty filters

You can hide filters until they have options.

For it you need set $hideWhenEmpty or call hideWhenEmpty() method:

class CityFilter extends DependentFilter
{
    public $hideWhenEmpty = true;
    
}
function filters(Request $request) {
    return [
        StateFilter::make('State', 'state_id'),
        
        CityFilter::make('City', 'city_id')->hideWhenEmpty(),
    ];
}

Default filter value

If you want to set default value you need to call withDefault method with value or overload default method in class declaration.

function filters(Request $request) {
    return [
        StateFilter::make('State', 'code')->withDefault('WA'),
    ];
}
class StateFilter extends DependentFilter
{
    public function default()
    {
        return 'WA';
    }
}

Other stuffs

By default filter checking by equal filed specified in $attribute and filter value. You can overload it like as in Nova filters:

class MyFilter extends DependentFilter
{
    public function apply(Request $request, $query, $value)
    {
        return $query->where('column', '>=', $value);
    }
}

Also you can specify another filter key over method key.

Thanks

Thanks to Brian for his support and advices.