title: return data from whereHas query in Laravel
body: <p>How would I go about returning the <code>select</code> data from the <code>company</code> <code>whereHas</code> sub-query?</p>
  
  <pre><code>$data = Job::where('active','=','1')
      -&gt;whereHas('company', function($q) use ($distance_select) {
          $q-&gt;where('active','=','1')
          -&gt;select(DB::raw($distance_select))
              -&gt;whereHas('user', function($q) {
                  $q-&gt;whereHas('group', function ($q) {
                      $q-&gt;whereIn('group.name_short', array('admin', 'moderator', 'subscriber'));
                  });
              });
      });
  </code></pre>
  
  <p>I get all the data from the <code>Job</code> model. My raw query returns a <code>distance</code> value from the <code>company</code> sub-query.</p>
  
  <p>Here is the sub-query for those interested.</p>
  
  <pre><code>if ( $units == 'miles' ) {
      $gr_circle_radius = 3959;
  } elseif ( $units == 'kilometers' ) {
      $gr_circle_radius = 6371;
  }
  
  $distance_select = sprintf(
      "
        ROUND(( %d * acos( cos( radians(%s) ) " .
      " * cos( radians( lat ) ) " .
      " * cos( radians( lng ) - radians(%s) ) " .
      " + sin( radians(%s) ) * sin( radians( lat ) ) " .
      " ) " .
      ")
                          , 2 ) " .
      "AS distance
                          ",
      $gr_circle_radius,
      $lat,
      $lng,
      $lat
  );
  </code></pre>
  
  <p>Everything works fine, I just want to be able to access the <code>distance</code> returned in the sub-query.</p>
  
  <p>------------added------------</p>
  
  <p>here is output of raw query
  </p>
  
  <pre><code>select * from `job` where `active` = '1' and (select ROUND(( 3959 * acos( cos( radians(38.15499960) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-85.66808610) ) + sin( radians(38.15499960) ) * sin( radians( lat ) ) ) ) , 2 ) AS distance from `company` where `job`.`company_id` = `company`.`id` and `active` = '1' and (select count(*) from `users` where `company`.`user_id` = `users`.`id` and (select count(*) from `group` inner join `group_user` on `group`.`id` = `group_user`.`group_id` where `group_user`.`user_id` = `users`.`id` and `group`.`name_short` in ('admin', 'moderator', 'subscriber')) &gt;= 1 and `users`.`deleted_at` is null) &gt;= 1) &gt;= 1 limit 10
  </code></pre>