F
Filament11mo ago
Vp

Passing parameters to extracted filter (for DRY)

I have date (from - to) filter and I used all of my application, but I want to extract and follow DRY If I try like below then I got this error unresolveable (Flare link https://flareapp.io/share/xPQZOxk7)
public static function filter($column) // working if remove $column
{
return Tables\Filters\Filter::make($column) // working if I put 'created_at'
->form([
Forms\Components\DatePicker::make('from')
->native(false),

Forms\Components\DatePicker::make('to')
->native(false),
])
->query(function (Builder $query, array $data, string $column): Builder { // working if remove $column
return $query
->when(
$data['from'],
fn (Builder $query, $date, $column): Builder => $query->whereDate($column, '>=', $date), // working if i remove $column from fn() and use 'created_at' in whereDate
)
->when(
$data['to'],
fn (Builder $query, $date, $column): Builder => $query->whereDate($column, '<=', $date),
);
});
}

// Call from resources like this
\App\Filament\Common\Filter::filter('created_at') // or updated_at, working without passing data
public static function filter($column) // working if remove $column
{
return Tables\Filters\Filter::make($column) // working if I put 'created_at'
->form([
Forms\Components\DatePicker::make('from')
->native(false),

Forms\Components\DatePicker::make('to')
->native(false),
])
->query(function (Builder $query, array $data, string $column): Builder { // working if remove $column
return $query
->when(
$data['from'],
fn (Builder $query, $date, $column): Builder => $query->whereDate($column, '>=', $date), // working if i remove $column from fn() and use 'created_at' in whereDate
)
->when(
$data['to'],
fn (Builder $query, $date, $column): Builder => $query->whereDate($column, '<=', $date),
);
});
}

// Call from resources like this
\App\Filament\Common\Filter::filter('created_at') // or updated_at, working without passing data
How can I pass dynamic data in my custom filter? If I use created_at instead of variable then it's working fine
2 Replies
Want results from more Discord servers?
Add your server