title: Laravel 4 Eloquent Query Builder - Complicated joins with variable
body: <p>I'm trying to replicate a join like so using the laravel query builder:</p>
  
  <pre><code>LEFT JOIN content_userdata
  ON content_id = content.id
  AND user_id = $user_id
  </code></pre>
  
  <p>I have discovered that I can do additional "ons" using the following function in my model which extends Eloquent</p>
  
  <pre><code>public function scopeJoinUserData($query, $user_id)
  {
      return $query-&gt;leftJoin('content_userdata', function($join)
      {
          $join-&gt;on('content_userdata_content_id', '=', 'content.content_id')-&gt;on('content_userdata_user_id', '=', 10);
      });
  }
  </code></pre>
  
  <p>But this creates two problems. Firstly I cannot get the $user_id variable into the function and secondly even if I hardcode it for testing purposes as I have done above (to int "10") Laravel encases it in ` meaning that it is interpreted as a column name when it shouldn't be, like so:</p>
  
  <pre><code>left join `content_userdata`
  on `content_id` = `content`.`id`
  and `user_id` = `10`
  </code></pre>
  
  <p>So now I have two problems.</p>
  
  <ol>
  <li>I cannot get the $user_id into the join function when using query scopes</li>
  <li>Even if I could I cannot send a variable to the join since it always interprets it as a column name</li>
  </ol>
  
  <p><strong>Why would I want to do this?</strong>
  I realise one response may be to place it in a where. However I am trying to do it this way as the join may not necessarily return any results (hence the left join), since the content_userdata table contains things like a users rating for a piece of content. If I use a where then results with nothing in the content_userdata table will not be returned, where as if I can put it in the join then they will be returned due to the left join.</p>
  
  <p>Is there anyway to achieve this in Laravel and if not what are the alternatives, obviously completely changing ditching Laravel is over the top but the only alternative I can think of is to get the userdata in a separate query.</p>