2016-02-05 107 views
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_itemscart_modifier_items添加和循環使用eloquent將它們添加到我的表?

cartcart Table 所有cart_itemscart_items Table 所有cart_modifier_itemscart_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, 
     ]); 
    } 
} 

回答

0

首先確保您的字段在相應模型中的可填充數組中。

// let save the item Record 
// am seeing an id in the object so lets update if id exist or 
// create if it does not 

$id = isset($request->input('item.id')) ? $request->input('item.id') : null; 
$cart = Cart::updateOrCreate(['id'=>$id],$request->input('item')) 

// now let save or update cart_items 
foreach($request->input('item.cart_items') as $cart_item){ 
    $cart_item_id = isset($cart_item['id']) ? $cart_item['id'] : null; 
    $cartItem = $cart->cart_items()->updateOrCreate(['id'=>$cart_item_id],$cart_item); 

    // now let save for cart_modifier_items 
    foreach($cart_item['cart_modifier_items'] as $cart_modifier_item){ 
     $cart_modifier_item_id = isset($cart_modifier_item['id']) ? $cart_modifier_item['id'] : null; 
     $cartModifierItem = $cartItem->cart_modifier_items()->updateOrCreate(['id'=>$cart_modifier_item_id],$cart_modifier_item); 
    } 

} 

你有一個想法,如何循環你的記錄和更新或創建記錄。接下來可能遇到的唯一問題是如何處理已從前端刪除/刪除的已保存數據。它由你來處理。乾杯。

+0

我不明白你的代碼。請檢查我更新的問題 –

+0

對不起,我沒有看到你的代碼。剛剛看到它。你的對象的關鍵是Cart,所以你爲什麼要做$ request-> input('item')。我想它應該是$ request-> input('Cart'),如果我理解正確的話。 – oseintow

+0

它被稱爲'item',因爲我從'angularJS'發送它爲'item' –