1 month ago

Select SUM from aliased column

Posted 1 month ago by mashkovtsev

I'm trying to get calculated values for every model relation row and then calculate a sum of these values. Here's my query for a better understanding:

Order::select(DB::raw(', orders.ref'))
->with(['OrderItems' => function ($q) {
		DB::raw(', orders_items.order_id, orders_items.quantity, orders_items.part_master_id, (orders_items.quantity*part_masters.declared_value) as total_value')
	->rightJoin('part_masters', 'orders_items.part_master_id', '=', '');

Order has many OrderItems. My goal is to get sum of Orderitems total_value for every order. Nothing helps here, so I decided to ask a community. Hope my explanation is clear. Thanks.

Please sign in or create an account to participate in this conversation.