PHP code example of dpsoft / nova-multiselect-filter

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

    

dpsoft / nova-multiselect-filter example snippets


use Dpsoft\NovaMultiselectFilter\MultiselectFilter;

class BooksByAuthorFilter extends MultiselectFilter
{
    public function apply(Request $request, $query, $value)
    {
        return $query->whereHas('books', function ($query) use ($value) {
            $query->whereIn('author_id', $value);
        });
    }

    public function options(Request $request)
    {
        return Authors::all()->pluck('name', 'id');
    }
}

use Dpsoft\NovaMultiselectFilter\MultiselectField;

public function fields(Request $request)
{
    return [
        MultiselectField::make('Categories')
            ->options([
                'tech' => 'Technology',
                'business' => 'Business', 
                'science' => 'Science'
            ])
            ->placeholder('Select categories'),
            
        MultiselectField::make('Tags')
            ->options(Tag::all()->pluck('name', 'id'))
            ->singleSelect() // For single selection
            ->max(3), // Maximum 3 selections
    ];
}

use Dpsoft\NovaMultiselectFilter\MultiselectFilter;

class UsersByRoleFilter extends MultiselectFilter
{
    public function __construct()
    {
        $this->model(\App\Models\Role::class)
             ->searchColumn('name')
             ->minChars(2)
             ->debounce(300);
    }

    public function apply(Request $request, $query, $value)
    {
        return $query->whereIn('role_id', $value);
    }
}

    public function options(Request $request)
    {
        return [
          'cat' => ['label' => 'Cat', 'group' => 'Pets'],
          'dog' => ['label' => 'Dog', 'group' => 'Pets'],
          'eagle' => ['label' => 'Eagle', 'group' => 'Birds'],
          'parrot' => ['label' => 'Parrot', 'group' => 'Birds'],
        ]
    }