Apply date filter Globally to aggregate result in table
Hello, everyone! I'm working with Laravel Filament and facing a challenge in dynamically filtering and aggregating data within a table column definition based on URL parameters. Specifically, I want to apply a date range filter from URL parameters to count and sum each customer's total orders and prices within that range. The filter is used globally, and now I need to reflect this filtered context within individual table columns for aggregate values.
note my date filter code is
Filter::make('scheduled_date_time')
->form([
Forms\Components\DatePicker::make('created_from'),
Forms\Components\DatePicker::make('created_until'),
])
->query(function (Builder $query, array $data): Builder {
return $query->whereHas('orders', function (Builder $query) use ($data) {
$query->when(
$data['created_from'],
fn (Builder $query, $date): Builder => $query->whereDate('scheduled_date_time', '>=', $date)
)->when(
$data['created_until'],
fn (Builder $query, $date): Builder => $query->whereDate('scheduled_date_time', '<=', $date)
);
});
});
Based on that, I want the total order and total count of users in that date range.
Tables\Columns\TextColumn::make('total_order')
->default(function (Customer $record) {
// Need to apply filtered date range here for counting and summing
return $record->orders->count(); // How to include filtering?
})
->label('Total Order');
0 Replies