dev-jm/nova-dependent-filter
Composer 安装命令:
composer require dev-jm/nova-dependent-filter
包简介
Cascading/dependent select filters for Laravel Nova 5.
README 文档
README
Cascading/dependent select filters for Laravel Nova 5. When a user selects a value in a parent filter, the child filter automatically narrows its options.
Demo
In this example, selecting the client INTLXS automatically narrows the Project filter to only show projects that belong to that client. Then, selecting a project narrows the User filter to only show users assigned to that specific project. Clearing a parent filter resets its children back to showing all available options.
Installation
Install via Composer:
composer require dev-jm/nova-dependent-filter
The package auto-registers its service provider via Laravel's package discovery.
Quick Start
use DevJM\DependentFilter\Nova\Filters\DependentFilter; use App\Models\Client; use App\Models\Project; use App\Models\User; public function filters(NovaRequest $request) { $client = DependentFilter::make('Client', Client::class, 'client_id'); $project = DependentFilter::make('Project', Project::class, 'project_id') ->dependsOn($client, foreignKey: 'client_id'); $user = DependentFilter::make('User', User::class, 'user_id') ->dependsOn($project, relationship: 'projects'); return [$client, $project, $user]; }
That's it. Three lines, three cascading filters.
How It Works
- Page loads - all filters show their full list of options.
- User picks a Client - the Project dropdown re-fetches and only shows projects belonging to that client.
- User picks a Project - the User dropdown re-fetches and only shows users assigned to that project.
- User clears a parent - child filters reset and go back to showing all options.
API Reference
DependentFilter::make(name, model, column)
Creates a new filter instance.
| Argument | Type | Description |
|---|---|---|
name |
string |
Display name shown in the filter panel |
model |
string |
Eloquent model class used to fetch options |
column |
string |
Column on the resource table to filter by (e.g. project_id) |
DependentFilter::make('Project', Project::class, 'project_id')
->dependsOn($parentFilter, foreignKey: ..., relationship: ...)
Declares that this filter's options depend on another filter's selected value. Use one of the two named arguments:
Option A: foreignKey (for belongsTo / direct column)
When the child model has a foreign key column pointing to the parent.
Client (id) <--- Project (client_id)
$project = DependentFilter::make('Project', Project::class, 'project_id') ->dependsOn($client, foreignKey: 'client_id');
This generates: Project::where('client_id', $selectedClientId)
Option B: relationship (for belongsToMany / hasMany)
When the child model is connected to the parent through an Eloquent relationship.
Project <---> User (pivot table: project_user)
$user = DependentFilter::make('User', User::class, 'user_id') ->dependsOn($project, relationship: 'projects');
This generates: User::whereHas('projects', fn($q) => $q->where('id', $selectedProjectId))
The
relationshipvalue must match the Eloquent relationship method name on the child model (e.g.User::projects()).
->scope(fn($query, $request) => ...)
Adds a base query scope applied to both the initial options and the dependent (filtered) options. Use this for permission checks, active status, or any global filtering.
// Only show active users with roles $user = DependentFilter::make('User', User::class, 'user_id') ->scope(fn ($query) => $query->whereHas('roles')->where('is_active', 1)); // Permission-based scoping $project = DependentFilter::make('Project', Project::class, 'project_id') ->scope(function ($query, $request) { if ($request->user()->hasPermissionTo('viewAllProjects')) { // no restriction } else { $query->whereHas('users', fn ($q) => $q->where('users.id', $request->user()->id)); } });
The callback receives
($query, $request). The second argument$requestis optional - omit it if you don't need it.
->label(column) and ->value(column)
Customize which model columns are used for the dropdown label and value.
| Method | Default | Description |
|---|---|---|
->label() |
'name' |
Column shown as the dropdown text |
->value() |
'id' |
Column sent as the selected value |
DependentFilter::make('Country', Country::class, 'country_code') ->label('full_name') ->value('iso_code');
Examples
Standalone filter (no dependencies)
Works like a regular Nova select filter - no cascading, just a cleaner syntax.
DependentFilter::make('Status', Status::class, 'status_id')
Two-level cascade
$department = DependentFilter::make('Department', Department::class, 'department_id'); $employee = DependentFilter::make('Employee', Employee::class, 'employee_id') ->dependsOn($department, foreignKey: 'department_id') ->scope(fn ($q) => $q->where('is_active', true)); return [$department, $employee];
Three-level cascade
$country = DependentFilter::make('Country', Country::class, 'country_id'); $city = DependentFilter::make('City', City::class, 'city_id') ->dependsOn($country, foreignKey: 'country_id'); $office = DependentFilter::make('Office', Office::class, 'office_id') ->dependsOn($city, foreignKey: 'city_id'); return [$country, $city, $office];
Conditional inclusion
$client = DependentFilter::make('Client', Client::class, 'client_id'); $project = DependentFilter::make('Project', Project::class, 'project_id') ->dependsOn($client, foreignKey: 'client_id'); if ($request->user()->isClient()) { return [$project]; } else { return [$client, $project]; }
Mixed with regular Nova filters
return [ (new StartsDateFilter())->setColumn('start'), (new EndsDateFilter())->setColumn('start'), $clientFilter, new TimeTrackingDepartmentFilter(), $projectFilter, $userFilter, ];
License
MIT
dev-jm/nova-dependent-filter 适用场景与选型建议
dev-jm/nova-dependent-filter 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 94 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「nova」 「laravel」 「dependent」 「cascading」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dev-jm/nova-dependent-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dev-jm/nova-dependent-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dev-jm/nova-dependent-filter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova card that shows you your system information.
A Laravel Nova card.
Nova searchable filter for belongsTo relationships.
A Laravel Nova package for publishable fields
A Laravel Nova package for translatable fields
Symfony DataGridBundle
统计信息
- 总下载量: 94
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-14
