'Laravel createMany on createMany while preserving relation

I have the same issue as this question asked. However, I would like to insert the data on the nested createMany with it's relation, and not insert multiple entries like:

id   question_id   answer
1    1             Good
2    1             Meh
3    2             Good
4    2             Meh

So if the question_id is 1 and the answer on that question is Good and for question_id 2, the answer on that question is Meh so that result should be:

id   question_id   answer
1    1             Good
2    2             Meh

Basically what I have is:

  • A User hasMany orders
  • An Order hasMany sub orders
  • A SubOrder hasMany sub order products

Here are the relations:

User Model:

class User extends Model
{
    public function orders() {
        return $this->hasMany(Order::class);
    }

}

Order Model:

class Order extends Model
{
    public function user() {
        return $this->belongsTo(User::class);
    }

    public function subOrders() {
        return $this->hasMany(SubOrder::class);
    }

}

SubOrder Model:

class SubOrder extends Model
{
    public function order() {
        return $this->belongsTo(Order::class);
    }

    public function subOrderProducts() {
        return $this->hasMany(SubOrderProducts::class);
    }

}

SubOrderProducts Model:

class SubOrderProducts extends Model
{
    public function subOrder() {
        return $this->belongsTo(SubOrder::class);
    }
}

Here are the tables:

orders table:

id   name

sub_orders table:

id   order_id   date   price

sub_order_products table:

id   sub_orders_id   product_id

Then on the store method:

$order = auth()->user()->orders()->create($request->validated());
$order_subs = $order->subOrders()->createMany($request->orders);
foreach ($order_subs as $order_sub) {
    $order_sub->subOrderProducts()->createMany($request->orders);
}

The $request->orders data is:

{
  "name": "My Order",
  "orders": [
    {
      "date": "2022-05-17",
      "product_id": [1],
      "price": 1
    },
    {
      "start_date": "2022-05-18",
      "product_id": [2],
      "price": 2
    }
  ]
}

This means that the order has two sub orders, with their corresponding products. I end up getting:

orders table:

id   name
1    Super Order

sub_orders table:

id   order_id   date          price
1    1          2022-05-17    1
2    1          2022-05-18    2

sub_order_products table:

id   sub_orders_id   product_id
1    1               1
2    1               2
3    2               1
4    2               2

My desired output would be to preserve the relation on the sub_order_products table, save and match the correct IDs. That would be:

sub_order_products table:

id   sub_orders_id   product_id
1    1               1
2    2               2

I'm thinking to change request->orders data and have some IDs for the sub_orders_id or maybe nest the createMany methods but I can't figure out how to. How to achieve this? Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source