sixteenhands/filament-dynamic-filter
Composer 安装命令:
composer require sixteenhands/filament-dynamic-filter
包简介
Dynamic table filters for Filament with caching, indicators and panel access control
README 文档
README
Dynamic select filters for Filament tables. For direct columns, options come from the current table query; relationship filters query the related model by default.
The Problem
If you've used DataTables before, you'll remember the column header filters that automatically populated with values
from the table data. Filament's built-in SelectFilter requires you to manually define options or query them separately
from an entire table/model.
This becomes a real pain with relationship managers. Say you have an Order resource with a LineItems relation
manager. You want to filter line items by product name, but only show products that actually exist in this order's
line items — not every product in the database.
This package gives you filters that pull their options from the current table query for direct columns, respecting any
existing filters or scopes already applied. Relationship filters default to querying the related model directly, but you
can override this with optionsQuery when you need custom constraints.
Installation
composer require sixteenhands/filament-dynamic-filter
Usage
use Filament\Tables\Contracts\HasTable; use Illuminate\Database\Eloquent\Builder; use SixteenHands\FilamentDynamicFilter\DynamicFilter; public function table(Table $table): Table { return $table ->columns([ TextColumn::make('packhouse.packhouse_name'), TextColumn::make('grade'), TextColumn::make('variety.variety_name'), ]) ->filters([ // Basic filter - column path matches query column DynamicFilter::make( name: 'packhouse_filter', column: 'packhouse.packhouse_name', queryColumn: 'packhouses.packhouse_name' ), // With options DynamicFilter::make( name: 'grade_filter', column: 'grade', queryColumn: 'grade', label: 'Grade', searchable: true ), // With custom option labels (booleans, enums, etc.) DynamicFilter::make( name: 'active_filter', column: 'is_active', queryColumn: 'is_active', optionsMap: [ 1 => 'Active', 0 => 'Inactive', ], ), // With a custom options query DynamicFilter::make( name: 'status_filter', column: 'status', queryColumn: 'status', optionsQuery: fn (HasTable $livewire, Builder $query) => $query->whereNotNull('status') ), ]); }
Multiple Selection
DynamicFilter::multiple( name: 'variety_filter', column: 'variety.variety_name', queryColumn: 'varieties.variety_name', searchable: true )
Lazy Option Loading (Searchable Only)
When lazy: true and searchable: true, options are empty until the user types. Search results are fetched on demand.
Large lists (recommended):
DynamicFilter::make( name: 'customer_filter', column: 'customer.name', queryColumn: 'customers.name', searchable: true, lazy: true )
Short lists (preload is fine):
DynamicFilter::make( name: 'status_filter', column: 'status', queryColumn: 'status', searchable: true )
Relationship Filters
For filtering through relationships using whereHas:
DynamicFilter::relationship( name: 'supplier_filter', column: 'supplier.name', relationship: 'supplier', relationshipColumn: 'name', multiple: true )
By default, relationship filters query the related model directly (no joins) so options and search work out of the box.
You can override this with optionsQuery if you need custom constraints:
DynamicFilter::relationship( name: 'supplier_filter', column: 'supplier.name', relationship: 'supplier', relationshipColumn: 'name', optionsQuery: fn (HasTable $livewire, Builder $query) => $query ->getModel() ->supplier() ->getRelated() ->newQuery() ->where('is_active', true) )
Panel Access Control
Restrict filters to specific panels:
DynamicFilter::make( name: 'admin_only_filter', column: 'internal_code', queryColumn: 'internal_code', panels: ['admin'] // Only shows in admin panel )
How It Works
The filter grabs the current table query (with all existing filters/scopes applied), plucks distinct values for the specified column when possible, and uses those as select options. Results are cached per column with a configurable TTL and scope.
Handles Carbon dates and PHP enums automatically — dates display as d/m/Y but filter as Y-m-d, enums use their
getLabel() method for display.
Parameters
DynamicFilter::make() and DynamicFilter::multiple()
| Parameter | Type | Description |
|---|---|---|
name |
string | Filter name (must be unique) |
column |
string | Dot-notation path to pluck from results (e.g. relation.field) |
queryColumn |
string | Database column for the where clause (e.g. table.field) |
placeholder |
?string | Select placeholder text |
label |
?string | Filter label |
searchable |
bool | Enable search in select (default: false) |
lazy |
bool | Defer options until search (requires searchable; default: false) |
panels |
?array | Restrict to specific panel IDs |
optionsMap |
?array | Map of raw values to display labels |
formatOption |
?callable | Formatter callback: `fn ($value): array |
optionsQuery |
?callable | Provide a custom options Builder or Collection: fn (HasTable $livewire, Builder $query) (distinct/limit apply to Builders) |
DynamicFilter::relationship() adds:
| Parameter | Type | Description |
|---|---|---|
relationship |
string | Relationship name for whereHas |
relationshipColumn |
string | Column within the relationship |
multiple |
bool | Allow multiple selection (default: false) |
It also supports all base parameters above (including optionsQuery).
Configuration
Publish the config if you'd like to tune caching:
php artisan vendor:publish --tag="filament-dynamic-filter-config"
return [ 'cache_ttl' => 300, 'max_options' => null, 'cache_scope' => 'user', // user | tenant | global ];
When using tenant scope, provide a tenant key or resolver in your config (otherwise caching is skipped for safety):
'tenant_key' => null, 'tenant_resolver' => fn () => tenant('id'),
Caching Notes
- Options are cached per column using a distinct query when possible.
- If a distinct query fails, the filter falls back to
$query->get()->pluck($column). - Exceptions do not write to cache; they return an empty options list.
max_optionscaps the number of options returned.
Requirements
- PHP 8.2+
- Filament 4.x or 5.x
License
MIT
Credits
sixteenhands/filament-dynamic-filter 适用场景与选型建议
sixteenhands/filament-dynamic-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「laravel」 「tables」 「filament」 「sixteenhands」 「filament-dynamic-filter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sixteenhands/filament-dynamic-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sixteenhands/filament-dynamic-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sixteenhands/filament-dynamic-filter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
(ru) language pack for the Static Info Tables providing localized names for countries, currencies and so on.
Nova searchable filter for belongsTo relationships.
(Ukrainian) language pack for the Static Info Tables providing localized names for countries,currencies and so on.
Turkish (tr) language pack for the Static Info Tables providing localized names for countries.
Display multiple images as a stack in your Filament tables
Symfony DataGridBundle
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-31