title: Laravel Dynamic Polymorphic Relation
body: <p>So I'm trying to make dynamic relationships for order items in Laravel. I have 3 main types of models:</p>
  <ul>
  <li>Order</li>
  <li>Order Item (has <code>orderable_type</code> and <code>orderable_id</code> that refer to Product models)</li>
  <li>Product (multiple models)</li>
  </ul>
  <p>And my goal is to have the Order model to be able to have order items but also include their respective product models (orderable).</p>
  <p>These are my models:</p>
  <pre><code>class Order extends Model
  {
      public function items()
      {
          return $this-&gt;hasMany(OrderItem::class);
      }
  }
  
  class OrderItem extends Model
  {
      public function order()
      {
          return $this-&gt;belongsTo(Order::class);
      }
  
      public function orderable()
      {
          return $this-&gt;morphTo();
      }
  }
  
  class ProductType extends Model
  {
      public function orderItem()
      {
          return $this-&gt;morphOne(OrderItem::class, 'orderable');
      }
  }
  </code></pre>
  <p>So, when I call <code>Order::with('items')-&gt;get()</code> I get the collection of <code>\App\Models\OrderItem</code> included:</p>
  <pre><code>Illuminate\Database\Eloquent\Collection {#4059
    all: [
      App\Models\Order {#4016
        id: 1,
        ...
        items: Illuminate\Database\Eloquent\Collection {#4026
          all: [
            App\Models\OrderItem {#4063
              id: 1,
              orderable_type: &quot;ProductType&quot;,
              orderable_id: 1,
              ...
            },
          ],
        },
      },
    ],
  }
  </code></pre>
  <p>And what I expect to have is:</p>
  <pre><code>Illuminate\Database\Eloquent\Collection {#4059
    all: [
      App\Models\Order {#4016
        id: 1,
        ...
        items: Illuminate\Database\Eloquent\Collection {#4026
          all: [
            App\Models\OrderItem {#4063
              id: 1,
              orderable_type: &quot;ProductType&quot;,
              orderable_id: 1,
              orderable: App\Models\Products\ProductType {
                ...
              }
              ...
            },
          ],
        },
      },
    ],
  }
  </code></pre>
  <p>So it's kind of a nested <code>with()</code>, but not sure how to make it happen. The purpose is to have everything in a single Eloquent call instead of calling the relationship afterward (meant to be on an API Resource as well).</p>