sixteenhands/filament-dynamic-filter

Dynamic table filters for Filament with caching, indicators and panel access control

Maintainers

Package info

github.com/16hands/filament-dynamic-filter

pkg:composer/sixteenhands/filament-dynamic-filter

Fund package maintenance!

16hands

Statistics

Installs: 6

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 4


README

Latest Version on Packagist Total Downloads

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_options caps the number of options returned.

Requirements

  • PHP 8.2+
  • Filament 4.x or 5.x

License

MIT

Credits

16Hands