Is it possible to apply a filter from an action?

I need to create a shortcut filter. I came up with the idea of defining an Action in headerActions. So I am wondering, if there is any possibility to apply filter from an action or possibly modify the eloquent query.
->headerActions([
Action::make('Show Client Translations')
->button()
->action(function () {
// Apply filter
// OR modify table eloquent query
}),
])
->headerActions([
Action::make('Show Client Translations')
->button()
->action(function () {
// Apply filter
// OR modify table eloquent query
}),
])
4 Replies
Mansoor Khan
Mansoor KhanOP2y ago
This is what i came up with for now:
->headerActions([
Action::make('Toggle Translations')
->button()
->label(
static::isTranslationFilterSetToClient()
? 'Show Laravel/Filament Translations'
: 'Show Client Translations'
)
->url(function () {
$query = request()->query();

$value = static::isTranslationFilterSetToClient()
? 'filament'
: 'client';

Arr::set($query, 'tableFilters.group.value', $value);

return url()->current().'?'.Arr::query($query);
}),
])

protected static function isTranslationFilterSetToClient(): bool
{
$query = request()->query();

return Arr::has($query, 'tableFilters.group.value')
&& Arr::get($query, 'tableFilters.group.value') === 'client';
}
->headerActions([
Action::make('Toggle Translations')
->button()
->label(
static::isTranslationFilterSetToClient()
? 'Show Laravel/Filament Translations'
: 'Show Client Translations'
)
->url(function () {
$query = request()->query();

$value = static::isTranslationFilterSetToClient()
? 'filament'
: 'client';

Arr::set($query, 'tableFilters.group.value', $value);

return url()->current().'?'.Arr::query($query);
}),
])

protected static function isTranslationFilterSetToClient(): bool
{
$query = request()->query();

return Arr::has($query, 'tableFilters.group.value')
&& Arr::get($query, 'tableFilters.group.value') === 'client';
}
Dan Harrin
Dan Harrin2y ago
You can try
->action(function ($livewire) {
$value = ...

$livewire->tableFilters['group']['value'] = $value;
})
->action(function ($livewire) {
$value = ...

$livewire->tableFilters['group']['value'] = $value;
})
Kenneth Sese
Kenneth Sese2y ago
@rumi In case you’re interested I’ve created a plug-in to do just this: give users quick access to filter setting right above the table. (And forgive the shameless plug, just thought it might help.) https://filamentphp.com/plugins/filter-sets
Filament
Filter Sets by Kenneth Sese - Plugins - Filament
Filament Filter Sets lets you save your existing filters, search query, column order, and column search queries into one easily accessible filter set.
Mansoor Khan
Mansoor KhanOP2y ago
Thanks for the help Dan!
Want results from more Discord servers?
Add your server