samita
samita

Reputation: 175

How to insert the value into query?

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

Answers (4)

Priyank Panchal
Priyank Panchal

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

VIKAS KATARIYA
VIKAS KATARIYA

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

Muhammad Faran Ali
Muhammad Faran Ali

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

Ajay Kumar Oad
Ajay Kumar Oad

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

Related Questions