title: How do you filter the user with hasRole() in Laravel 5 within relational query?
body: <p>I want to filter my query and return only if the user hasRole "Premium" and limit the result to 10.</p>
  
  <p>Along with this query is a count of records which Conversion has and sort it in DESC order by total column.</p>
  
  <p>Right now I have a working query that returns the count of Conversion and with a User but without user filter Role.</p>
  
  <pre><code>// Model Conversion belongs to User
  // $from &amp; $end uses Carbon::createDate()
  // Current code
  $query = Conversion::select('user_id',DB::raw('COUNT(*) as total'))
                  -&gt;whereBetween('created_at', [$from,$end])
                  -&gt;where('type','code')
                  -&gt;where('action','generate')
                  -&gt;whereNotNull('parent_id')
                  -&gt;with('user')
                  -&gt;groupBy('user_id')
                  -&gt;orderBy('total', 'DESC')
                  -&gt;take(10)
                  -&gt;get();
  
  // current result
  foreach ($query as $q) {
      $q-&gt;user-&gt;name; // To access user's name
      $q-&gt;total; // To access total count
  }
  
  // I tried this but no luck
  $query = Conversion::select('user_id',DB::raw('COUNT(*) as total'))
                  -&gt;whereBetween('created_at', [$from,$end])
                  -&gt;where('type','code')
                  -&gt;where('action','generate')
                  -&gt;whereNotNull('parent_id')
                  -&gt;with('user', function($q) {
                      $q-&gt;hasRole('Premium');
                  })
                  -&gt;groupBy('user_id')
                  -&gt;orderBy('total', 'DESC')
                  -&gt;take(10)
                  -&gt;get();
  </code></pre>