I have a dashboard with several widgets. There are 4 stats widgets and 2 table widgets. So I have created a page filter to handle filtering data for all widgets. Following the docs on Filtering widget data it works perfectly for the stats widgets.
However, the table widget doesn't;t work as I need it to because its summing data from a rational table.
The table shows the top 10 results from the Website model based on Orders from each website.
Website model contains HasMany orders. And it displays perfectly without the filter. But when I select the data, it filters out Websites that have had orders within the date range, rather than filtering out the orders and value. - I hope that makes sense.
The code below is what I am using which is using the use InteractsWithPageFilters; trait. What I need to do is to filter the values in the columns. Any help would be great. thanks in advance.
return $table
->query(
Website::whereBelongsTo(auth()->user()->company)
->when($startDate, fn (Builder $query) => $query->whereHas('orders', function ($q){
$startDate = $this->filters['startDate'] ?? null;
return $q->whereDate('order_placed_at', '>=', $startDate);
}))
->when($endDate, fn (Builder $query) => $query->whereHas('orders', function ($q){
$endDate = $this->filters['endDate'] ?? null;
return $q->whereDate('order_placed_at', '<=', $endDate);
}))
->limit(10)
)
->columns([
TextColumn::make('name'),
TextColumn::make('orders_count')
->counts('orders')
->label('Total orders'),
TextColumn::make('orders_sum_total_price')
->sum('orders', 'total_price')
->label('Total revenue')
->money('gbp'),
])
->defaultSort('orders_count', 'desc')
->heading('Top 10 websites by order volume')
->striped()
->paginated(false);