title: Laravel - Select in select in query builder
body: <p>How can I make in Laravel Querybuilder Select in selected data?</p>
  
  <p>I have</p>
  
  <pre><code>DB::table('notifuserdoc')-&gt;select('nud_user', 'nud_document', 'name', 'surname', DB::raw('count(nud_id) as count'))
              -&gt;join('user', 'user.id', '=', 'notifuserdoc.nud_user')
              -&gt;groupBy('nud_user', 'nud_document')
              -&gt;having('count', '&gt;', 1)
              -&gt;get();
  </code></pre>
  
  <p>which return data like:</p>
  
  <p><a href="https://i.stack.imgur.com/utD0A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/utD0A.png" alt="enter image description here"></a></p>
  
  <p>But now, I want just write out user name and number of rows:</p>
  
  <pre><code>User 1 - 1
  User 2 - 6
  User 3 - 3
  </code></pre>
  
  <p>How can i get this result with Query builder?</p>
  
  <p>** <strong>EDIT:</strong> **</p>
  
  <p><strong>I found the sollution:</strong></p>
  
  <pre><code>$sub = DB::table('notifuserdoc')-&gt;select('nud_document', 'nud_user', DB::raw('count(nud_id) as dc'))
              -&gt;whereNotNull('nud_user')
              -&gt;groupBy('nud_user', 'nud_document')
              -&gt;having('dc', '&gt;', 1);
  
  $other = DB::table( DB::raw("({$sub-&gt;toSql()}) as sub") )-&gt;select(
          'nud_user',
          'name', 
          'surname',
          DB::raw('count(dc) as count')
      )
      -&gt;mergeBindings($sub)
      -&gt;join('user', 'user.id', '=', 'sub.nud_user')
      -&gt;groupBy('nud_user')
      -&gt;orderBy('count', 'DESC')
      -&gt;get();
  </code></pre>