I have a lumen collection object for my order and use map function to iterate over it and do some logic. I also need the total value of the order which is calculated using quantity * price. But the variable that is responsible for holding the total value is always 0.
$total = 0; $items = Cart_Item::where('user_id', $user->id)->get(); $items = $items->map(function ($item, $key) use ($order, $total) { $product = Product::where('id', $item->product_id)->first(); $order_item = new Order_Item(); $order_item->order_id = $order->id; $order_item->product_id = $item->product_id; $order_item->quantity = $item->quantity; $order_item->price = $product->GetPrice->price; $order_item->save(); $total = $total + ($order_item->quantity * $order_item->price); }); No matter what I do, the $total always returns 0.