Newbie question about reusing filters

Just under 10 hours using Fillament, so maybe obvious and I am missing it, but is there a common Filament way of reusing filters or this is just fine?
<?php

namespace App\Filament\Resources\Shared\Filters;

use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;

abstract class UpcomingFilter
{
public static function make(): Filter
{
return Filter::make('upcoming')
->translateLabel('Upcoming')
->query(fn (Builder $query): Builder => $query->where('published_at', '>', now()))
->toggle();
}
}
<?php

namespace App\Filament\Resources\Shared\Filters;

use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;

abstract class UpcomingFilter
{
public static function make(): Filter
{
return Filter::make('upcoming')
->translateLabel('Upcoming')
->query(fn (Builder $query): Builder => $query->where('published_at', '>', now()))
->toggle();
}
}
Basically creating a class and pulling it from there in any resource which uses that filter.
Solution:
You’re on the right track and this will probably work. But I would extend the Filter class itself instead on returning a filter instance. Then you can define the modifiers inside the setUp() method. And then you can just use it in your table as UpcomingFilter::make() without having to give it a name.
Jump to solution
2 Replies
Solution
awcodes
awcodes8mo ago
You’re on the right track and this will probably work. But I would extend the Filter class itself instead on returning a filter instance. Then you can define the modifiers inside the setUp() method. And then you can just use it in your table as UpcomingFilter::make() without having to give it a name.
reppair
reppairOP8mo ago
Just for reference if someone is looking for it in the future, here is the refactored version of the above class which works fine too but is indeed more elegant.
<?php

namespace App\Filament\Resources\Shared\Filters;

use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;

class UpcomingFilter extends Filter
{
public static function getDefaultName(): ?string
{
return 'upcoming';
}

protected function setUp(): void
{
parent::setUp();

$this->translateLabel('Upcoming')
->query(fn (Builder $query): Builder => $query->where('published_at', '>', now()))
->toggle();
}
}
<?php

namespace App\Filament\Resources\Shared\Filters;

use Filament\Tables\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;

class UpcomingFilter extends Filter
{
public static function getDefaultName(): ?string
{
return 'upcoming';
}

protected function setUp(): void
{
parent::setUp();

$this->translateLabel('Upcoming')
->query(fn (Builder $query): Builder => $query->where('published_at', '>', now()))
->toggle();
}
}
Want results from more Discord servers?
Add your server