title: Laravel Eloquent WHERE field=(...) subquery
body: <p>I have three tables: (Many-To-Many relation between users and wordlists, and userlistrels is connector)</p>
  
  <pre><code>users:
  id
  name
  
  wordlists:
  id
  name
  creator_id
  
  userlistrels:
  id
  user_id
  wordlist_id
  </code></pre>
  
  <p>I want to execute following query:</p>
  
  <pre><code>SELECT * FROM users WHERE id=( SELECT wordlists.creator_id FROM userlistrels JOIN wordlists ON wordlists.id=userlistrels.wordlist_id WHERE userlistrels.user_id=$curr_user )
  </code></pre>
  
  <p>How to write this type of subqueries (<code>WHERE foo= ( subquery )</code>) in Laravel interface?<br>
  Documentation covers <code>SELECT * FROM (subquery) WHERE contition</code>,<br> <code>SELECT * FROM table WHERE (subquery LIMIT 1) = 'foo'</code>,<br>
  but not <code>SELECT * FROM table WHERE field=(subquery)</code></p>
  
  <p>I've tried many approaches, and this is the only one that doesn't throw exception, however it doesn't work:</p>
  
  <pre><code>$curr_user = // id of my user
  $users = User::where('id', function($query) use($curr_user){
          $query-&gt;select('wordlists.creator_id')
              -&gt;from('userlistrels')
              -&gt;join('wordlists', 'wordlists.id', '=', 'userlistrels.wordlist_id')
              -&gt;where('userlistrels.user_id', $curr_user);
      })-&gt;get();
  </code></pre>
  
  <p>Could anyone please tell me what's wrong with this code, or how to make this type of subquery?</p>
  
  <p>EDIT:
  My SQL query was wrong.
  Correct query for what i tried to do is:
  <code>SELECT * FROM users JOIN (SELECT wordlists.creator_id FROM userlistrels JOIN wordlists ON wordlists.id=userlistrels.wordlist_id WHERE userlistrels.user_id=$curr_user) AS nn ON users.id=nn.creator_id;</code></p>
  
  <p>And correct laravel code:</p>
  
  <pre><code>$users_sub = DB::table('userlistrels')-&gt;join('wordlists', 'wordlists.id', '=', 'userlistrels.wordlist_id')-&gt;where('userlistrels.user_id', $curr_user)-&gt;select('wordlists.creator_id');
  
          $users = DB::table('users')
              -&gt;joinSub($users_sub, 'tt', function ($join) {
                  $join-&gt;on('users.id', '=', 'tt.creator_id');
              })-&gt;get();
  </code></pre>