Eloquent循环数组并添加到DB - Laravel / Eloquent

时间:2016-02-05 11:51:43

标签: laravel laravel-5 eloquent

我刚开始掌握eloquent魔杖我通过angularJS

将以下物品发送到我的后端
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
              }
            ]
        ]
    }

正如您所看到的,我的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,
        ]);
    }
}

1 个答案:

答案 0 :(得分: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);
   }

}

您知道如何循环记录并更新或创建记录。您接下来可能遇到的唯一问题是如何处理已从前端删除/删除的已保存数据。由你来处理。欢呼声。