Filters
Filters are returned from the component's filters() method:
public function filters(): array
{
return [
// Filter definitions...
];
}
Every filter has a key, label, input type, optional default value, and a callback that can modify the Eloquent query.
Common filter methods
TextFilter::make('name')
->label('Name')
->filter(fn (Builder $query, $value) => ...);
make(string $key)
The key normally corresponds to a database column or a value used by the custom callback.
label(string $label)
Sets the label shown next to the filter control.
filter(Closure $callback)
The callback receives the current Builder and filter value:
->filter(function (Builder $query, $value): void {
if ($value !== null && $value !== '') {
$query->where('name', 'like', '%'.$value.'%');
}
})
Default values
Set the initial filter value through the public value property:
$filter = TextFilter::make('name');
$filter->value = 'John';
TextFilter
use RealZone22\PenguTables\Table\Filters\TextFilter;
TextFilter::make('name')
->label('Name')
->filter(fn (Builder $query, $value) =>
$query->where('name', 'like', '%'.$value.'%')
);
TextFilter renders a text input. It does not automatically apply a query
condition when a callback is not supplied; define the callback explicitly.
DateFilter
use RealZone22\PenguTables\Table\Filters\DateFilter;
DateFilter::make('created_at')
->label('Created at')
->filter(fn (Builder $query, $value) =>
$query->whereDate('created_at', $value)
);
DateFilter renders a date input. The callback is responsible for deciding
whether to use whereDate, a range, or another date condition.
SelectFilter
use RealZone22\PenguTables\Table\Filters\SelectFilter;
SelectFilter::make('role')
->label('Role')
->options([
'admin' => 'Administrator',
'user' => 'User',
])
->filter(fn (Builder $query, $value) =>
$query->where('role', $value)
);
options() receives an associative array of option values and labels. An
empty “all” option is added automatically.
BooleanFilter
use RealZone22\PenguTables\Table\Filters\BooleanFilter;
BooleanFilter::make('email_verified_at')
->label('Email verified')
->allLabel('Any')
->trueLabel('Verified')
->falseLabel('Not verified');
The built-in behavior treats true as a non-null column and false as a
null column. The labels can be customized with allLabel(), trueLabel(),
and falseLabel().
MultiSelectFilter
use RealZone22\PenguTables\Table\Filters\MultiSelectFilter;
MultiSelectFilter::make('role')
->label('Roles')
->options([
'admin' => 'Administrator',
'user' => 'User',
])
->filter(fn (Builder $query, $values) =>
$query->whereIn('role', $values)
);
MultiSelectFilter uses an array of selected values. It is currently marked
deprecated because it only works with the old PenguBlade multi-select
component. Prefer a custom filter integration when using a different UI
component.
Resetting filters
The default table view provides controls for resetting all filters and for removing individual active filters. The component methods are:
$this->resetFilters();
$this->removeFilter('name');