sixteenhands / filament-dynamic-filter
Dynamic table filters for Filament with caching, indicators and panel access control
Package info
github.com/16hands/filament-dynamic-filter
pkg:composer/sixteenhands/filament-dynamic-filter
Fund package maintenance!
Requires
- php: ^8.2
- filament/tables: ^4.0|^5.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^8.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^9.0|^10.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/laravel-ray: ^1.26
- dev-main
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-dependabot/github_actions/dependabot/fetch-metadata-3.0.0
- dev-dependabot/github_actions/ramsey/composer-install-4
- dev-dependabot/github_actions/actions/checkout-6
- dev-dependabot/github_actions/stefanzweifel/git-auto-commit-action-7
This package is auto-updated.
Last update: 2026-03-28 20:52:29 UTC
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