Sum of 2 columns of a relation

I have a model Order which hasMany orderPositions. A order position contains the columns product_price and amount.

How can I now show the "value" of an order in a table. With value I mean the sum of all orderPositions prices times it amounts.

I tried to use the aggregate functions:

TextColumn::make('order_positions_sum_product_price')
  ->money("EUR")
  ->sum('orderPositions', 'product_price'),


But this gives me obviously only the sum of the prices and ignores the amount of the positions.

TextColumn::make('order_positions_sum_product_price')
  ->money("EUR")
  ->sum([
      'orderPositions' => fn (Builder $query) => $query->selectRaw('(product_price * amount) as product_price'),
  ], 'product_price'),


doesnt change anything? Any ideas?
Solution
Nevermind, found it:

TextColumn::make('order_positions_sum_product_price')
    ->money("EUR")
    ->sum(
        ['orderPositions' => fn (Builder $query) => $query->select(\DB::raw('SUM(product_price * amount)'))],
        'product_price'
    ),
Was this page helpful?