degecko/laravel-nova-multifilter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

degecko/laravel-nova-multifilter

最新稳定版本:v1.0.0

Composer 安装命令:

composer require degecko/laravel-nova-multifilter

包简介

Combine multiple filter columns into a single Nova filter panel

README 文档

README

Combine multiple filter columns into a single Nova filter panel. Supports select dropdowns, text search, date ranges, number ranges, bitwise flags, and custom filter handlers.

Installation

composer require degecko/laravel-nova-multifilter

The service provider and assets are registered automatically via Laravel's package discovery.

Building Assets

npm install
npm run prod

During development:

npm run watch

Usage

In your Nova resource's filters() method:

use DeGecko\NovaMultiFilter\MultiFilter;

public function filters(NovaRequest $request): array
{
    return [
        new MultiFilter('Search', [
            'name' => '%w%',                    // LIKE search (word-boundary aware)
            'email' => '%#%',                    // LIKE search (contains)
            'status' => ['active', 'inactive'],  // Select dropdown
            'created_at' => ['from' => null, 'to' => null], // Date range
        ]),
    ];
}

Column Types

Select Dropdown

Pass an array of options. Use a flat array for value-only options, or an associative array for value => label pairs:

// Flat array — values are used as both the option value and label
'status' => ['active', 'inactive', 'banned'],

// Associative — keys are stored, values are displayed
'role' => ['admin' => 'Administrator', 'editor' => 'Editor', 'user' => 'User'],

Text Search (LIKE)

Use a string pattern where # is replaced with the user's input:

'name' => '%w%',    // Word-boundary search (splits on non-alphanumeric)
'email' => '%#%',   // Simple contains
'phone' => '#%',    // Starts with

The %w% pattern is special: it splits the input on non-alphanumeric characters and wraps each part with %, giving word-boundary-aware matching. For example, searching "John Doe" becomes %John%Doe%.

Date Range

Pass from/to keys to render two date pickers. Either bound is optional — leaving one empty creates an open-ended range:

'created_at' => ['from' => null, 'to' => null],
'updated_at' => ['from' => null, 'to' => null],

Number Range

Add 'type' => 'range' to render number inputs instead of date pickers:

'age' => ['from' => null, 'to' => null, 'type' => 'range'],
'price' => ['from' => null, 'to' => null, 'type' => 'range'],

Bitwise Flags

For integer columns storing bitwise flags. Labels are auto-formatted from the keys:

'permissions' => ['bitwise', [
    'can_edit' => 1,
    'can_delete' => 2,
    'can_publish' => 4,
]],

Column Aliases

Prefix a column with a different display name using as:

'email as E-mail' => '%#%',
'created_at as Registered' => ['from' => null, 'to' => null],

The part before as is used for the query; the part after is displayed as the label.

Composing with Nova Filters

You can pass existing Nova filter instances directly as columns. Their options will be extracted and their apply() method will be called when the value changes:

use App\Nova\Filters\StatusFilter;
use App\Nova\Filters\CategoryFilter;

new MultiFilter('Filters', [
    'status' => new StatusFilter,
    'category' => new CategoryFilter,
    'name' => '%w%',
]),

Custom Handlers

Register custom handler callbacks for columns that need special query logic:

(new MultiFilter('Search', [
    'name' => '%w%',
    'active' => ['Yes', 'No'],
]))->handlers([
    'active' => fn($value, $query) => $query->where('is_active', $value === 'Yes'),
]),

Defaults

Set default filter values that are applied on page load:

(new MultiFilter('Filters', [
    'status' => ['active', 'inactive', 'banned'],
    'role' => ['admin', 'editor', 'user'],
]))->defaults([
    'status' => 'active',
]),

Query Tap

Apply a scope or constraint to all filtered queries via the tap parameter:

// Scope to current tenant
new MultiFilter('Search', $columns, tap: fn($q) => $q->where('tenant_id', auth()->user()->tenant_id)),

// Include soft-deleted records
new MultiFilter('Search', $columns, tap: fn($q) => $q->withTrashed()),

Debugging

Chain ->log() to output the raw SQL query to the Laravel log after filters are applied:

(new MultiFilter('Search', $columns))->log(),

Full Example

use DeGecko\NovaMultiFilter\MultiFilter;
use App\Nova\Filters\StatusFilter;

public function filters(NovaRequest $request): array
{
    return [
        (new MultiFilter('User Search', [
            'name' => '%w%',
            'email as E-mail' => '%#%',
            'phone' => '#%',
            'status' => new StatusFilter,
            'role' => ['admin' => 'Admin', 'editor' => 'Editor', 'user' => 'User'],
            'created_at as Registered' => ['from' => null, 'to' => null],
            'age' => ['from' => null, 'to' => null, 'type' => 'range'],
            'permissions' => ['bitwise', ['can_edit' => 1, 'can_delete' => 2, 'can_publish' => 4]],
        ], tap: fn($q) => $q->where('tenant_id', auth()->user()->tenant_id)))
        ->defaults(['role' => 'user'])
        ->handlers([
            'custom' => fn($value, $query) => $query->whereHas('profile', fn($q) => $q->where('bio', 'like', "%$value%")),
        ])
        ->log(),
    ];
}

How It Works

The MultiFilter renders all columns in a single horizontal filter panel. Each column type (select, text, date range, bitwise) gets the appropriate input widget. Changes are debounced (100ms) and applied as a combined filter value.

On the backend, each column's value is applied to the query based on its type:

  • Arrays - where($column, $value) or date range with whereDate
  • Strings - where($column, 'like', $pattern)
  • Bitwise - whereRaw("$column & ?", [$value])
  • Handlers - delegated to the callback or Nova filter's apply() method

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固