F
Filament11mo ago
anjama

Creating table filter in a trait

I have a table filter that works perfectly when created directly in my resources, but fails when I try to move it to a trait (I have multiple resources with the same filter).
trait SoftDeleteFilter
{
public static function softDeleteFilter()
{
return Filter::make('is_deleted')
->label('Deleted')
->baseQuery(fn (Builder $query) => $query->withoutGlobalScopes([
SoftDeleteScope::class,
]))
->toggle()
->query(function (Builder $query, array $data): Builder {
return $query->whereHas('activity_log', fn (Builder $query) => $query->where('activity', '=', ActionEnum::Delete));
});
}
}
trait SoftDeleteFilter
{
public static function softDeleteFilter()
{
return Filter::make('is_deleted')
->label('Deleted')
->baseQuery(fn (Builder $query) => $query->withoutGlobalScopes([
SoftDeleteScope::class,
]))
->toggle()
->query(function (Builder $query, array $data): Builder {
return $query->whereHas('activity_log', fn (Builder $query) => $query->where('activity', '=', ActionEnum::Delete));
});
}
}
How I'm calling it:
->filters([
self::softDeleteFilter()
])
->filters([
self::softDeleteFilter()
])
Doesn't cause an error; just basically clears the table. I suspect I'm messing up something related to static methods, but I'm not sure. Can anyone point out what my mistake is? Would it be a better approach to just extend the Filter class similar to how the trashed filter extends the ternary filter class? Thanks
Solution:
Laravel macros are new to me. While trying to find documentation on them, I eventually came across Dan Harrin's recent laracasts series, and it turns out one of his videos creates a custom filter and turns it into a reusable class like I was thinking about https://laracasts.com/series/build-advanced-components-for-filament/episodes/11. So I'm going to go with that approach, but now that I know about Laravel macros, I'll definitely be spending some time learning them. Edit: while creating the class, I realized I forgot to import my SoftDeleteScope class into the trait. I didn't realize that would fail silently....
Jump to solution
2 Replies
LeandroFerreira
LeandroFerreira11mo ago
You could create a macro in the AppServiceProvider
Table::macro('withSoftDeleteFilter', fn () =>
$this->filters([
Filter::make('is_deleted')
...
])
);
Table::macro('withSoftDeleteFilter', fn () =>
$this->filters([
Filter::make('is_deleted')
...
])
);
return $table
...
->withSoftDeleteFilter()
return $table
...
->withSoftDeleteFilter()
Solution
anjama
anjama11mo ago
Laravel macros are new to me. While trying to find documentation on them, I eventually came across Dan Harrin's recent laracasts series, and it turns out one of his videos creates a custom filter and turns it into a reusable class like I was thinking about https://laracasts.com/series/build-advanced-components-for-filament/episodes/11. So I'm going to go with that approach, but now that I know about Laravel macros, I'll definitely be spending some time learning them. Edit: while creating the class, I realized I forgot to import my SoftDeleteScope class into the trait. I didn't realize that would fail silently.
Want results from more Discord servers?
Add your server