PHP code example of resham / nova-dependent-filter

1. Go to this page and download the library: Download resham/nova-dependent-filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

resham / nova-dependent-filter example snippets


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');
    }
}

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');
    }
}

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');
    }
}

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

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