php - 从 Laravel 中的 whereHas 查询返回数据

标签 php mysql laravel laravel-4 eloquent

我将如何从 company whereHas 子查询返回 select 数据?

$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

最佳答案

经过数小时的研究后弄明白了。

这是新的工作代码

$data = Job::where('job.active','=',1)
    ->select(DB::raw("`job`.*, `company`.`lat` as `lat`, `company`.`lng` as `lng`, ROUND(( ? * acos( cos( radians(?) )  * cos( radians( `lat` ) )  * cos( radians( `lng` ) - radians(?) )  + sin( radians(?) ) * sin( radians( `lat` ) )  ) ) , 2 ) AS `distance`"))
    ->leftJoin('company', function($join) {
      $join->on('job.company_id', '=', 'company.id');
    })
    ->setBindings([$radius, $lat, $lng, $lat], 'select')
    ->whereHas('company', function($q) {
        $q->where('active','=',1)
        ->whereHas('user', function($q) {
            $q->whereHas('group', function ($q) {
                $q->whereIn('group.name_short', array('admin', 'moderator', 'subscriber'));
            });
        });
    });

这是感兴趣的人的原始查询

select `job`.*, `company`.`lat` as `lat`, `company`.`lng` as `lng`, 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 `job` left join `company` on `job`.`company_id` = `company`.`id` where `job`.`active` = '1' and (select count(*) 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

关于php - 从 Laravel 中的 whereHas 查询返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28402191/

相关文章:

php - 按国家 ip 范围阻止用户注册

php - 如果 Count 为零,如何在 Laravel 中获取记录

php artisan Schedule :run, 没有计划的命令准备好运行,不知道为什么?

php - 找不到类 App\Articles

php - 最佳查询 - 有什么方法可以避免循环中的数百个查询?

php - Symfony 2.6 安全 BCryptPasswordEncoder 错误

javascript - 如何在 Javascript 上添加 onChange 事件以仅重新加载选择选项

php - 如何在不直接访问文件的情况下在网站上提供可下载的发票?

php - 如何在 View 中显示数据

mysql - Laravel 4 where like 子句