PHP code example of dkulyk / nova-dependent-filter

1. Go to this page and download the library: Download dkulyk/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/ */

    

dkulyk / nova-dependent-filter example snippets


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

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

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

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

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
                        ],
                    ];
                });
            }),
    ];
}

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

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

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

class CityFilter extends DependentFilter
{
    public $hideWhenEmpty = true;
    
}

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


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


class StateFilter extends DependentFilter
{
    public function default()
    {
        return 'WA';
    }
}


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