定制 dpsoft/nova-multiselect-filter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dpsoft/nova-multiselect-filter

最新稳定版本:v1.2

Composer 安装命令:

composer require dpsoft/nova-multiselect-filter

包简介

Multiselect filter and field for Laravel Nova.

README 文档

README

Latest Version on Packagist Total Downloads

This Laravel Nova package adds a multiselect to Nova's filters and form fields.

Requirements

  • laravel/nova: ^3.0

Features

  • Multi select
  • Single select
  • Group select
  • Search
  • Filter and Field

Installation

Install the package in a Laravel Nova project via Composer:

composer require dpsoft/nova-multiselect-filter

Usage

As a Filter

The filter can be used when switching Filter class with MultiselectFilter.

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

As a Form Field

The field can be used in Nova resources for form input:

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

Built-in server-side search endpoint

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

Option groups

Option groups are supported. Their syntax is the same as Laravel's option group syntax.

In this example (from Nova docs), all values are grouped by the group key:

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

Options

Possible options you can pass to the filter using the option name as a function, ie ->placeholder('Choose peanuts').

Option type default description
options Array|callable [] Options in an array as key-value pairs (['id' => 'value']).
placeholder String Field name The placeholder string for the input.
max Number Infinite The maximum number of options a user can select.
groupSelect Boolean false For use with option groups - allows the user to select whole groups at once
singleSelect Boolean false Makes the field act as a single select which also means the saved value will not be an array.
optionsLimit Number 1000 The maximum number of options displayed at once. Other options are still accessible through searching.
ajaxEndpoint String null Custom endpoint to search and return options for async search. Defaults to /nova-vendor/nova-multiselect-filter/search.
ajaxMethod String get Request method for async search.
ajaxParam String search Query param name for the search term.
debounce Number 300 Debounce in milliseconds for async search.
minChars Number 0 Minimum characters before search triggers.
model String null Fully qualified model class name used by the built-in search endpoint.
searchColumn String null Column name used by the built-in search endpoint.
limit Number null The number of options to return from a server-side search.

Credits

This package was inspired by klepak/nova-multiselect-filter

License

This project is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 53
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: Vue

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-11