title: Laravel relationship problems
body: <p>I've got 4 tables:</p>
  
  <p><img src="https://i.stack.imgur.com/CTIpA.jpg" alt="structure"></p>
  
  <p>My relationships should work like this:</p>
  
  <p>Items can only have one size, color and category.</p>
  
  <p>This should be working but it's really not. The query generated returns wrong results.</p>
  
  <p>Here are my model files:</p>
  
  <pre><code>&lt;?php
  
  class Shop_Item extends Eloquent
  {
      public static $table = 'items';
      public static $timestamps = false;
  
      public function scategory() {
          return $this-&gt;has_one('Shop_Category','id');
      }
  
      public function ssize() {
          return $this-&gt;has_one('Shop_Size','id');
      }
  
      public function scolor() {
          return $this-&gt;has_one('Shop_Color','id');
      }
  }
  </code></pre>
  
  <p>The rest of the model files for the remaining tables are the same (except table name and model name).</p>
  
  <pre><code>&lt;?php
  class Shop_Category extends Eloquent
  {
      public static $table = 'item_categories';
      public static $timestamps = false;
  }
  </code></pre>
  
  <p>So when I try to access the extra values (size->name, color->name, category->name), I get wrong results. </p>
  
  <p>I have two test records in my database:
  Item 1 and Item 2 with different color, size and category.
  Item 1 is blue and have the size of M, 
  Item 2 is green and have the size of XL, but not in the returned query. Which shows me, that Item 2 is red and have the size of S.</p>
  
  <p>Controller:</p>
  
  <pre><code>&lt;?php
  
  class Admin_Shop_Controller extends Base_Controller {
  
      public function action_index() {
  
          $items = Shop_item::order_by('name')-&gt;paginate(10,array('id','name','price','sex','visible','color','size','category'));
          return View::make('admin.shop.index')-&gt;with('items', $items);
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@forelse($items-&gt;results as $i)
  {{ $i-&gt;name }}
  {{ $i-&gt;price }}
  {{ $i-&gt;sex }}
  {{ $i-&gt;scategory-&gt;name }}
  {{ $i-&gt;scolor-&gt;name }}
  {{ $i-&gt;ssize-&gt;name }}
  
  &lt;a href = "{{ URL::to('admin/shop/edit/'.$i-&gt;id) }}"&gt;Edit&lt;/a&gt;
  &lt;a href = "#"&gt;Delete&lt;/a&gt;
  
  @empty
      There are no items in the shop.
  @endforelse
  </code></pre>
  
  <p>Queries generated:</p>
  
  <pre><code>0.23ms  
  SELECT COUNT(`id`) AS `aggregate` FROM `items`
  0.28ms  
  SELECT `id`, `name`, `price`, `sex`, `visible`, `color`, `size`, `category` FROM `items` ORDER BY `name` ASC LIMIT 10 OFFSET 0
  0.25ms  
  SELECT * FROM `item_categories` WHERE `id` IN ('1', '2')
  0.21ms  
  SELECT * FROM `item_sizes` WHERE `id` IN ('1', '2')
  0.36ms  
  SELECT * FROM `item_colors` WHERE `id` IN ('1', '2')
  </code></pre>
  
  <p>Note that in the view if I access these values from the other table like this:</p>
  
  <pre><code>{{ Shop_Color::find($i-&gt;color)-&gt;name }}
  </code></pre>
  
  <p>It gets me the right result, but I really don't want to query the database n+3 times because of this. Any suggestions what am I doing wrong?</p>
  
  <p><strong>Edit:</strong> <em>Still no luck. :( I've done the changes you listed, experimented with them but this thing still not working. Current error is :</em></p>
  
  <pre><code>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'
  SQL: SELECT * FROM `item_colors` WHERE `id` IN (?)
  
  Bindings: array (
    0 =&gt; 0,
  )
  </code></pre>
  
  <p><em>I don't know why it looks for an id, I've changed all references to the child tables and renamed columns appropriately. :(</em></p>