How to show in a column from table sum from relationship
I have this:
Tables\Columns\TextColumn::make('order_items_sum_price')
->sum('orderItems', 'price')->money('eur')->sortable()->label('Precio Total')
But every orderItem has a column with amount.
I need to multiply the quantity by its price and in turn make the total sum. How could I do it?4 Replies
While there are several ways to tackle this problem, I'd suggest adding a virtual/stored generated column (for qty * price) to
order_items
.is this what you are trying to do ?
https://filamentphp.com/tricks/calculated-value-in-column
Filament
Calculated value in column by Juan Benitez - Tricks - Filament
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
Thank you so much! I have this:
Tables\Columns\TextColumn::make('order_items_sum_price')
->getStateUsing(function (Model $record) {
return $record->orderItems->sum(fn($item) => $item->quantity * $item->price);
})
I also thought about it, but I wanted to reduce fields in the database and not have to keep updatingGenerated stored columns automatically update, that's why I suggested π