title: Displaying product variants
body: <p>I have an online store that sells clothes. Each <code>items</code> (ex: T-shirt) have two attributes, <code>colors</code> and <code>sizes</code>. I'm combining the three tables together in a <code>variants</code> table, that has the following structure : </p>
  
  <pre><code>- item_id
  - color_id
  - size_id
  - price
  - stock
  </code></pre>
  
  <p>This works and I'm using a <code>hasMany()</code> relationship between the <code>items</code> and the <code>variants</code> (an item has many variants) and then a <code>belongsTo()</code> relationship between each variants and the colors/sizes (each variants belong to a color and to a size). </p>
  
  <p>Here's the code I'm using to fetch all the items : </p>
  
  <pre><code>$items = Item::with(['variants', 'variants.color', 'variants.size'])-&gt;get();
  </code></pre>
  
  <p>Here's a sample of the returned data : <a href="http://pastebin.com/CjsYp6wS" rel="nofollow">http://pastebin.com/CjsYp6wS</a></p>
  
  <p>The problem I have is with displaying this data. I want to display each item in a table, and for each item have two select boxes : one that shows all the colors, one that shows all the sizes. The user can then choose a color and a size, and see the updated price for this variant.</p>
  
  <p>However, because of the way my relationships are setup, if I want to display all the colors and all the sizes, I have to do something like this : </p>
  
  <pre><code>&lt;select name="color"&gt;
      @foreach($item-&gt;variants as $variant)
      &lt;option value="{{ $variant-&gt;color-&gt;id }}"&gt;{{ $variant-&gt;color-&gt;name }}&lt;/option&gt;
      @endforeach
  &lt;/select&gt;
  </code></pre>
  
  <p>This is far from ideal, since it results in duplicated values for each colors and sizes if they happen to be in two or more variants.</p>
  
  <p>Is there a better way to do this? I'm thinking that there has to be a simpler way to do it, with a different database structure or Eloquent relationships or something. </p>