Skip to content
Advertisement

how to sum values in below array in laravel controller?

i have a laravel project i want add these 3 values sum in a variable and store in b how i can do that?

$benefit = Order::where('ot_id', $order->ot_id)->sum('ot_benefit');
$a_ben = Order::where('ot_id', $order->ot_id)->sum('subtotal') - sum('ot_benefit') - sum('discount');
$updateData = ['ben_earned' => $benefit , 'a_ben' => $a_ben];

this query create error on sum function

Advertisement

Answer

The problem is in your second line. You cannot call the sum function like you do 3 times.

$benefit = Order::where('ot_id', $order->ot_id)->sum('ot_benefit'); 

$subtotal = Order::where('ot_id', $order->ot_id)->sum('subtotal'); 

$discount = Order::where('ot_id', $order->ot_id)->sum('discount'); 

$a_ben = $ot_benefit - $subtotal - $discount;

$updateData = ['ben_earned' => $benefit , 'a_ben' => $a_ben];
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement