0

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

2 Answers 2

2

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];
Sign up to request clarification or add additional context in comments.

1 Comment

oh thanx you r reight
0
$order = Order::select(
    DB:raw('SUM(subtotal) AS st'), 
    DB:raw('SUM(ot_benefit) AS ot'),
    DB:raw('SUM(discount) AS dt')
)
->where('ot_id', $order->ot_id)
->first();

And then:

$total = $order->st + $order->ot - $order->discount;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.