PHP code example of bourne / dependent-filter

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

    

bourne / dependent-filter example snippets


function filters(Request $request)
{
    return [
        (new DependentFilter('State'))
            ->withOptions([
                'all' => 'All orders',
                'dragt' => '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 = 'ctaegory_uid';
    
    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')
            ->dependentOf('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);
    }
}

function filters(Request $request) {
    return [
        StateFilter::make('State', 'code')->withApply(function ($request, $query, $value) {
            return $query->where('code', '=', $value);
        }),
    ];
}