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
ZedoX
ZedoXβ€’2y ago
While there are several ways to tackle this problem, I'd suggest adding a virtual/stored generated column (for qty * price) to order_items.
moaaz_m_f
moaaz_m_fβ€’2y ago
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.
Daniel Reales
Daniel RealesOPβ€’2y ago
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 updating
ZedoX
ZedoXβ€’2y ago
Generated stored columns automatically update, that's why I suggested πŸ™‚
Want results from more Discord servers?
Add your server