0
我有2個表格用於放置我的訂單。在我的orders
表中,我包含訂單的主要細節,在我的order_item
表中,我詳細列出了每個訂購的項目。這是我爲同一代碼:訂單對象未在收集的地圖功能中定義
$order = new Order();
$order->user_id = $user->id;
if(!$order->save()){
return response()->json([
'message' => 'Unable to place your order. Try again.'
], 500);
}
$items = Cart_Item::where('user_id', $user->id)->get();
if($items->isEmpty()){
return response()->json([
'message' => 'Kindly add items to the cart before placing an order.'
], 500);
}
$items = $items->map(function ($item, $key) {
$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;
if(!$order_item->save()){
return response()->json([
'message' => 'Unable to place your order. Try again.'
], 500);
}
});
當我嘗試執行此,我上線$order_item->order_id = $order->id;
得到這個錯誤Undefined variable: order
。爲什麼我不能訪問訂單對象?
這是你今天給我的第二個答案。萬分感謝:) – JackSlayer94