saurav77
3 weeks ago
195
6
Laravel

How to calculate the sum of values in an array ?

Posted 3 weeks ago by saurav77

When I do dd($request->all()); in controller. Result come like this

 "product_quantity" => array:2 [▼
    0 => "23"
    1 => "32"
  ],
"invoice_no" => 3
 "loading_port" => 22

This is Storing part

$order=DB::table('orders')->update([
'invoice_no'=$request->invoice_no,
'loading_port'=$request->loading_port,
'quantity'= array_sum($request->product_quantity),
])

Actually, I want to store quantity by the sum of all product quantity

Can we use array_sum() like this or is there any better method ?

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