1
我剛開始通過angularJS
雄辯循環,並加入到DB - Laravel /雄辯
item:
{
id: 1,
cookie_id: "c312b3faf70d3bc39b9563b340a1094bf6f58891",
user_id: null,
restaurant_id: 11,
restaurant_name: "Gaucho",
restaurant_uname: "gaucho",
city: "abuja",
location: "asokoro",
time: "12:45:00",
updated_at: "2016-02-05 11:37:05",
created_at: "2016-02-05 10:50:48",
cart_items: [
"id": 159,
"name": "Empanadas (Choice of 2)",
"description": "Choice of Diced Beef; Spinach, Stilton and Onion; or Smoked Ham and Mozzarella",
"price": 700,
"available": 1,
"created_at": "2016-01-31 16:50:31",
"updated_at": "2016-01-31 16:50:31",
"menu_category_id": 41,
"restaurant_id": 11,
"cart_modifier_items": [
{
"id": 34,
"name": "Diced Beef",
"price": 0,
"created_at": "2016-02-01 01:04:08",
"updated_at": "2016-02-01 01:04:08",
"menu_modifier_group_id": 9,
"restaurant_id": 11,
"menu_item_id": 159,
"selected": true
},
{
"id": 35,
"name": "Smoked Salmon & Mozzarella",
"price": 0,
"created_at": "2016-02-01 01:04:37",
"updated_at": "2016-02-01 01:04:37",
"menu_modifier_group_id": 9,
"restaurant_id": 11,
"menu_item_id": 159,
"selected": true
}
]
]
}
得到認真處理eloquent
魔杖我送下面的對象到我的後端,你可以看到我有一個parent
項目包含多個cart_items
這則包含多個cart_modifier_items
我的模型細節(3個表);
Cart
hasMany cart_items
CartItem
belongsTo Cart
hasMany cart_modifier_items
CartModifierItem
belongsTo CartItem
我怎麼能接受這個項目通過cart_items
和cart_modifier_items
添加和循環使用eloquent
將它們添加到我的表?
cart
到cart Table
所有cart_items
到cart_items Table
所有cart_modifier_items
到cart_modifier_items Table
我想我會需要3個獨立DB
請求?
任何意見/指導讚賞
這是我迄今爲止所做的;
public function addItem (Request $request) {
$item = $request->input('item');
$rest_id = $request->input('id');
$cookie_id = $request->input('basket');
$cart = Cart::where(['cookie_id' => $cookie_id, 'restaurant_id' => $rest_id])->get();
$cart_id = $cart->id;
$cart_items = $item['cart_items'];
foreach($cart_items as $cart_item) {
CartItem::create([
'item_id' => $cart_item['id'],
'restaurant_id' => $cart_item['restaurant_id'],
'name' => $cart_item['name'],
'description' => $cart_item['description'],
'price' => $cart_item['price'],
'qty' => $cart_item['qty'],
'available' => $cart_item['available'],
'cart_id' => $cart_id,
'cart_cookie' => $cookie_id,
]);
}
$cart_modifier_items = $cart_items['cart_modifier_items'];
foreach($cart_modifier_items as $cart_modifier_item) {
CartModifierItem::create([
'item_id' => $cart_modifier_item['id'],
'cart_item_id' => $cart_modifier_item['menu_item_id'],
'name' => $cart_modifier_item['name'],
'price' => $cart_modifier_item['price'],
'cart_id' => $cart_id,
]);
}
}
我不明白你的代碼。請檢查我更新的問題 –
對不起,我沒有看到你的代碼。剛剛看到它。你的對象的關鍵是Cart,所以你爲什麼要做$ request-> input('item')。我想它應該是$ request-> input('Cart'),如果我理解正確的話。 – oseintow
它被稱爲'item',因爲我從'angularJS'發送它爲'item' –