title: Laravel Eloquent Join 2 Tables
body: <p>These are my models:</p>
  
  <pre><code>class Branch extends Eloquent {
  
      protected $table = 'tb_branch';
  
      public function faculty() 
      {
          return $this-&gt;hasMany('Faculty');
      }
  }
  
  class Faculty extends Eloquent {
  
      protected $table = 'tb_faculty';
  
      public function branch() 
      {
          return $this-&gt;belongsTo('Branch');
      }
  
  }
  </code></pre>
  
  <p>In branch table I have set a column with foreign key <code>user_id</code> that links to users table.</p>
  
  <p>In faculty table I have set a column with foreign key <code>branch</code> which links to branch table with column <code>name</code>.</p>
  
  <p>And now I need to retrieve Faculty data based on currently logged in user (with Eloquent).</p>
  
  <p>I imagine the work flow would be: </p>
  
  <ul>
  <li>retrieve id of currently logged in user</li>
  <li>link that id to the branch's <code>user_id</code> and retrieve branch's <code>name</code></li>
  <li>link that branch's <code>name</code> to faculty branch's <code>name</code> and retrieve all matched faculty data</li>
  </ul>
  
  <p>Now this is what I have tried, it didn't work:</p>
  
  <pre><code>$user_id = Sentry::getUser();
  $faculty = Faculty::with('branch')-&gt;where('branch.user_id',$user-&gt;id)-&gt;get();
  </code></pre>
  
  <p>Error says:  </p>
  
  <blockquote>
    <p>Unknown column 'branch.user_id' in 'where clause' (SQL: select * from
    <code>tb_faculty</code> where <code>branch</code>.<code>user_id</code> = 6)</p>
  </blockquote>
  
  <p>How can I achieve this with Eloquent?</p>