vendredi 5 février 2016

Eloquent loop through array and add to DB - Laravel/Eloquent

I'm just starting to get to grips with eloquent wand I'm sending the below object to my back end via angularJS

Cart:
     {
        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
              }
            ]
        ]
    }

As you can see I have a parent item that contains multiple cart_items that then contain multiple cart_modifier_items

My model details (3 tables);

Cart 
hasMany cart_items

CartItem 

belongsTo Cart
hasMany cart_modifier_items

CartModifierItem

belongsTo CartItem

How can i accept this item add and loop through cart_items and cart_modifier_items and add them to me tables using eloquent?

cart to the cart Table all cart_items to the cart_items Table all cart_modifier_items to the cart_modifier_items Table

I guess I would need 3 seperate DB requests?

Any advice/guidance appreciated



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1VUblfW
via IFTTT

Aucun commentaire:

Enregistrer un commentaire