【发布时间】:2017-08-13 00:47:18
【问题描述】:
我的订单有一个流明集合对象,并使用 map 函数对其进行迭代并执行一些逻辑。我还需要使用quantity * price 计算的订单总值。但是负责保存总值的变量始终是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);
});
无论我做什么,$total 总是返回0。
【问题讨论】:
标签: php laravel lumen php-closures