Custom Filter Table

Hi, how to filter created_at by "Last month", "Last year" just like in this picture?
No description
6 Replies
Matthew
Matthew15mo ago
Are you sure you're using filament and not just laravel?
nuriniman93
nuriniman93OP15mo ago
Am I doing it right?
No description
No description
No description
Matthew
Matthew15mo ago
please follow the #✅┊rules when sending code Dont just send a screenshot ```php ```
nuriniman93
nuriniman93OP15mo ago
sorry for that.
public static function table(Table $table): Table
{
$table = $table
->filters([
SelectFilter::make('created_at')
->options([
'last_month' => 'Last Month',
'last_year' => 'Last Year',
])
->query(function (Builder $query) use ($user) {
$value = $user->selectedDateFilter;
return $this->applyDateFilter($query, $value);
}),
])
}

protected function applyDateFilter(Builder $query, $value): Builder
{
$now = now();

if ($value === 'last_month') {
$startDate = $now->subMonth()->startOfMonth();
$endDate = $now->subMonth()->endOfMonth();
} elseif ($value === 'last_year') {
$startDate = $now->subYear()->startOfYear();
$endDate = $now->subYear()->endOfYear();
}

return $query->whereBetween('created_at', [$startDate, $endDate]);
}
public static function table(Table $table): Table
{
$table = $table
->filters([
SelectFilter::make('created_at')
->options([
'last_month' => 'Last Month',
'last_year' => 'Last Year',
])
->query(function (Builder $query) use ($user) {
$value = $user->selectedDateFilter;
return $this->applyDateFilter($query, $value);
}),
])
}

protected function applyDateFilter(Builder $query, $value): Builder
{
$now = now();

if ($value === 'last_month') {
$startDate = $now->subMonth()->startOfMonth();
$endDate = $now->subMonth()->endOfMonth();
} elseif ($value === 'last_year') {
$startDate = $now->subYear()->startOfYear();
$endDate = $now->subYear()->endOfYear();
}

return $query->whereBetween('created_at', [$startDate, $endDate]);
}
the error is become like this:
No description
Want results from more Discord servers?
Add your server