/nova-dependent-filter

Dynamic Dependent Filter For Laravel Nova Version 4

Primary LanguagePHP

Dependent Filter for Laravel Nova 4

Latest Stable Version License Total Downloads

This is a package to integrate a dependent filter with Laravel Nova version 4.

Requirements

Installation

Require this package with composer.

composer require resham/nova-dependent-filter

Usage

Normal Usage

Once the Installation is complete, you can use it by creating a filter class.

use Resham\NovaDependentFilter\DependentFilter;

class CountryFilter extends DependentFilter
{
    /**
     * Name of filter.
     *
     * @var string
     */
    public $name = 'Country';

    /**
     * The filter's attribute. Also, it is key of filter.
     *
     * @var string
     */
    public $key = 'country_code';

    /**
     * Get the filter's available options.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @param  array $filters
     * @return array
     */
    public function options(NovaRequest $request, array $filters = [])
    {
        return Country::pluck('name', 'country_code');
    }
}

Dependent Usage

Let's have a dependent filter state that depends on the country, we can achieve the dynamic-dependent filter

Parent Filter

Define the parent filter(on which other filters depend)

use Resham\NovaDependentFilter\DependentFilter;

class CountryFilter extends DependentFilter
{
    /**
     * Name of filter.
     *
     * @var string
     */
    public $name = 'Country';

    /**
     * The filter's attribute. Also, it is key of filter.
     *
     * @var string
     */
    public $key = 'country_code';

    /**
     * The other filters key whose are depends on this filter.
     *
     * @var string[]
     */
    public $parentOf = ['state'];

    /**
     * Get the filter's available options.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @param  array $filters
     * @return array
     */
    public function options(NovaRequest $request, array $filters = [])
    {
        return Country::pluck('name', 'country_code');
    }
}

Note: Don't forget to define the key of the child filter on the $parentOf property.

Child Filter

Let's define the child filter which is depends on parent filter.

use Resham\NovaDependentFilter\DependentFilter;

class StateFilter extends DependentFilter
{
    /**
     * Name of filter.
     *
     * @var string
     */
    public $name = 'State';

    /**
     * The filter's attribute. Also, it is key of filter.
     *
     * @var string
     */
    public $key = 'state';

    /**
     * Get the filter's available options.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @param  array $filters
     * @return array
     */
    public function options(NovaRequest $request, array $filters = [])
    {
        return State::where('country_code', $filters['country_code'] ?? '')
                        ->pluck('name', 'id');
    }
}

Registering Filters

We can register the filter as we define with nova.

/**
 * Get the filters available for the resource.
 *
 * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
 * @return array
 */
public function filters(NovaRequest $request)
{
    return [
        new CountryFilter
    ];
}

Also, you can use CountryFilter::make()

/**
 * Get the filters available for the resource.
 *
 * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
 * @return array
 */
public function filters(NovaRequest $request)
{
    return [
        CountryFilter::make()
    ];
}

Security

If you discover any issues, please email at reshampokhrel57@gmail.com.

Reference