Reputation: 175
Do I need to insert the variable in sum(orderdets.quantity) into another variable?
$order = Order::with('customer','product')
->select(
'orders.id',
'orders.customer_id',
'orderdets.product_id',
DB::raw('SUM(orderdets.quantity)'))
->get();
Upvotes: 2
Views: 262
Reputation: 119
Please try below script in your controller.
$orders = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) as sum'))->get();
return $orders;
Thanks PHPanchal
Upvotes: 0
Reputation: 6005
Try This
$orders = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) as sum'))->get();
dd($orders);
Upvotes: 1
Reputation: 499
$orders = Order::with('customer','product')
->select('orders.id', 'orders.customer_id', 'orderdets.product_id'))
->get()->map(function($order){
$order->put('sum', $order->orderdets->sum('quantity');
})->save();
This will sum all your debts and will also update orders table in one go, Although you question wasn't clear but I think you must be looking for something like this
Upvotes: 0
Reputation: 560
Use AS tempName
to get the column data by specific name. this way it can be accessed.
$order = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) AS sumOrders'))->get();
Upvotes: 0