Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

moh_abk's avatar

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 I want to save the below object

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
              }
            ]
        ]
    }

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

0 likes
8 replies
d3xt3r's avatar

What is the problem where you are stuck? The object can be accessed from ?

$cart = $request->get('Cart');

dd($cart); // to check how you can access the components
moh_abk's avatar

I'm trying to save the object not retrieve it

d3xt3r's avatar

I can understand that from the OP. I am asking, where are you facing the problem?

moh_abk's avatar

I don't know how to loop through the cart_items and cart_modifiers_items and add them to DB

moh_abk's avatar

This is what I have so far

Is this what you advised? I haven't tested yet;

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_item['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,
            ]);
        }
    }
}
rikh's avatar

You'll need to set cart_item_id to the id of the CartItem model you created.

$cartItem = CartItem::create(blar);
foreach(modifiers blar) {
    // ...
    'cart_item_id' => $cartItem->id,
    // ...
}

Please or to participate in this conversation.