PHP code example of ptplugins / filament-auto-filters
1. Go to this page and download the library: Download ptplugins/filament-auto-filters library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ptplugins / filament-auto-filters example snippets
use PtPlugins\FilamentAutoFilters\Concerns\HasAutoFilters;
class EmployeeResource extends Resource
{
use HasAutoFilters;
public static function table(Table $table): Table
{
return $table
->columns([/* ... */])
->filters(static::autoFilters($table));
}
}
class Employee extends Model
{
// Direct columns
const NAME = 'name';
const EMAIL = 'email';
const HIRED_AT = 'hired_at';
const SALARY = 'salary';
// JSON data column fields
const D_POSITION = 'data.position';
const D_OFFICE = 'data.office';
// Relationships
const R_DEPARTMENT_NAME = 'department.name';
const R_MANAGER_NAME = 'manager.name';
protected $casts = [
'hired_at' => 'date',
'data' => 'array',
];
public function department(): BelongsTo
{
return $this->belongsTo(Department::class);
}
public function manager(): BelongsTo
{
return $this->belongsTo(Employee::class, 'manager_id');
}
}
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use PtPlugins\FilamentAutoFilters\Concerns\HasAutoFilters;
class EmployeeResource extends Resource
{
use HasAutoFilters;
protected static ?string $model = Employee::class;
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make(Employee::NAME)
->label('Name')
->searchable(),
TextColumn::make(Employee::EMAIL)
->label('Email'),
TextColumn::make(Employee::R_DEPARTMENT_NAME)
->label('Department'),
TextColumn::make(Employee::R_MANAGER_NAME)
->label('Manager'),
TextColumn::make(Employee::HIRED_AT)
->label('Hired')
->date(),
TextColumn::make(Employee::SALARY)
->label('Salary')
->numeric(),
TextColumn::make(Employee::D_POSITION)
->label('Position'),
TextColumn::make(Employee::D_OFFICE)
->label('Office'),
])
->filters(static::autoFilters($table));
}
}
->filters(static::autoFilters($table, overrides: [
// Replace the auto-generated text filter for department
// with a select dropdown instead
static::makeSelectFilter(
Employee::R_DEPARTMENT_NAME,
'Department',
Department::pluck('name', 'name')
),
// Custom filter with your own logic
SelectFilter::make(Employee::SALARY)
->label('Salary Range')
->options([
'junior' => 'Under 50k',
'mid' => '50k - 100k',
'senior' => 'Over 100k',
]),
]))
// Every plain text column becomes a multi-select of its distinct values,
// except the free-text ones you list
->filters(static::autoFilters($table,
distinct: true,
except: ['notes', 'description'],
))
// ...or only the columns you name (direct, JSON data.key or relation.column)
->filters(static::autoFilters($table,
distinct: ['cost', 'data.city', 'department.name'],
))
use Filament\Tables\Table;
public static function table(Table $table): Table
{
return $table
->columns([/* ... */])
->filters(static::autoFilters($table))
->filtersFormWidth('md') // sm | md | lg | xl
->filtersTriggerAction(
fn ($action) => $action->slideOver(), // open on the right
);
}