Laravel Eloquent查询建设

时间:2015-05-26 12:48:07

标签: php laravel laravel-4 php-5.4

我当前的查询是

select * from `users` where `group_id` = 1 and ((select count(*) from `works` inner join `user_work` on `works`.`id` = `user_work`.`work_id` where `user_work`.`user_id` = `users`.`id` and `work_duration_id` >= 3) >= 1)

对于这个查询,我在laravel 4.2中编写了以下雄辩的方法,它工作正常

$u = User::where('group_id', '=', 1);    
$u = $u->where(function($m) use($minWorkDuration,$searchCriteria){
     $m = $m->whereHas('work', function($q) use($minWorkDuration){
          $q->where('work_duration_id', '>=', $minWorkDuration );
     });
});

但我想稍微改变一下我的查询,这是

select * from `users` where `group_id` = 1 and ((select count(*) from `works` inner join `user_work` on `works`.`id` = `user_work`.`work_id` where `user_work`.`user_id` = `users`.`id` and (SELECT SUM(`work_duration_id`) from `works` group by `user_id`) >= 3) >= 1)

如果我想写这个查询,那么在eloquent方法中会有什么。如果有人在那里请建议我如何在laravel 4.2中用雄辩的方法编写这个查询。 提前谢谢。

0 个答案:

没有答案