title: Laravel How To Update Nested createMany
body: <p>Been having a hard time on figuring out how to implement updating records on a nested createMany based on the store method. I have these models and relations:</p>
  <p><strong>User Model:</strong></p>
  <pre><code>class User extends Model
  {
      public function orders() {
          return $this-&gt;hasMany(Order::class);
      }
  
  }
  </code></pre>
  <p><strong>Order Model:</strong></p>
  <pre><code>class Order extends Model
  {
      public function user() {
          return $this-&gt;belongsTo(User::class);
      }
  
      public function subOrders() {
          return $this-&gt;hasMany(SubOrder::class);
      }
  
  }
  </code></pre>
  <p><strong>SubOrder Model:</strong></p>
  <pre><code>class SubOrder extends Model
  {
      public function order() {
          return $this-&gt;belongsTo(Order::class);
      }
  
      public function subOrderProducts() {
          return $this-&gt;hasMany(SubOrderProducts::class);
      }
  
  }
  </code></pre>
  <p><strong>SubOrderProducts Model:</strong></p>
  <pre><code>class SubOrderProducts extends Model
  {
      public function subOrder() {
          return $this-&gt;belongsTo(SubOrder::class);
      }
  }
  </code></pre>
  <p><strong>Here are the tables:</strong></p>
  <p><strong>orders table:</strong></p>
  <pre><code>id   name
  </code></pre>
  <p><strong>sub_orders table:</strong></p>
  <pre><code>id   order_id   date   price
  </code></pre>
  <p><strong>sub_order_products table:</strong></p>
  <pre><code>id   sub_orders_id   product_id
  </code></pre>
  <p>Then on the <strong>store</strong> method:</p>
  <pre><code>public function store(StoreOrderRequest $request) {
      $order = auth()-&gt;user()-&gt;orders()-&gt;create($request-&gt;validated());
      $order_subs = $order-&gt;subOrders()-&gt;createMany($request-&gt;orders);
      $order_sub_products = $request-&gt;only('subproducts');
  
      foreach ($order_subs as $key =&gt; $value) {
          $order_sub-&gt;subOrderProducts()-&gt;createMany($order_sub_products[$key]['products']);
      }
  }
  </code></pre>
  <p>What I wanted to achieve is to update or create those <strong>sub orders</strong> and <strong>sub order products</strong>. If the <strong>sub order</strong> is already existing, then update, otherwise create a new record with those corresponding sub order products.</p>
  <p>Here's what I've tried so far:</p>
  <pre><code>public function store(UpdateOrderRequest $request, Order $order) {
      $order-&gt;update($request-&gt;validated());
      
      $order_subs = $order-&gt;subOrders()-&gt;updateMany($request-&gt;orders);
      $order_sub_products = $request-&gt;only('subproducts');
      
      foreach ($order_subs as $key =&gt; $value) {
          $order_sub-&gt;subOrderProducts()-&gt;updateMany($order_sub_products[$key]['products']);
      }
  }
  </code></pre>
  <p>It's not working as expected since the order subs is only a single record. What's the best way or the right process for updating/creating these records? Thanks.</p>