I have the following code, and it works as expected.
$details = OrderDetails::query()
->selectRaw('product_id, sum(quantity) as total_quantity')
->groupBy('product_id')
->orderBy('total_quantity', 'desc')
->pluck('product_id', 'total_quantity');
dump($details);
However the fun begins when I try to put this data into a Filament table widget. No matter what I do it keeps hitting me back with errors, here's what I've tried so far (among other billion things)
On app/Filament/Widgets/BestSellingProducts.php
1 :
public function table(Table $table): Table
{
return $table
->query(
OrderDetails::query()
->selectRaw('product_id, sum(quantity) as total_quantity')
->groupBy('product_id')
->orderBy('total_quantity', 'desc')
)
->columns([
Tables\Columns\TextColumn::make('product.name'),
Tables\Columns\TextColumn::make('total_quantity'),
]);
}
This gives the following error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'total_quantity' in 'order clause'
select
count(*) as aggregate
from
`order_details`
group by
`product_id`
order by
`total_quantity` desc,
`order_details`.`id` asc
2 :
public function table(Table $table): Table
{
return $table
->query(
OrderDetails::query()
->selectRaw('product_id, sum(quantity) as total_quantity')
->groupBy('product_id')
)
->columns([
Tables\Columns\TextColumn::make('product.name'),
Tables\Columns\TextColumn::make('total_quantity'),
]);
}
This gives the following error :
"SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'projectName.order_details.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"
select
count(*) as aggregate
from
`order_details`
group by
`product_id`
order by
`order_details`.`id` asc
There's a bunch of other things that I've tried and failed but this post would be 5 km long if I include all of them. Isn't there a way to just simply provide an array and make it a filament table?
The documentation on table widgets on filament docs website is ridiculous, it only contains the terminal command to create the widget (i.e. : You're gonna add --table to your command, BIG HELP)