1
我將如何去返回數據從company
whereHas
子查詢?從Laravel返回數據whereHas查詢
$data = Job::where('active','=','1')
->whereHas('company', function($q) use ($distance_select) {
$q->where('active','=','1')
->select(DB::raw($distance_select))
->whereHas('user', function($q) {
$q->whereHas('group', function ($q) {
$q->whereIn('group.name_short', array('admin', 'moderator', 'subscriber'));
});
});
});
我從Job
模型中獲取所有數據。我的原始查詢返回company
子查詢中的distance
值。
這是對那些感興趣的子查詢。
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
);
一切正常,我只是希望能夠訪問distance
在子查詢返回。
------------ ------------添加
這裏是原始查詢
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')) >= 1 and `users`.`deleted_at` is null) >= 1) >= 1 limit 10