例如,我有两个查询:
return $this->model->where('closed_at', null)
->whereHas('users', function ($query) {
$query->where('id', '=', auth()->user()->id);
})
->with(['product:id,title', 'in_works' => function ($query) {
$query->where('user_id', '=', auth()->user()->id);
}])
->get();
它工作正常,并通过join(急切加载)返回1个查询:
select * from `order` where `closed_at` is null and exists (select * from `user` inner join `user_order` on `user`.`id` = `user_order`.`user_id` where `order`.`id` = `user_order`.`order_id` and `id` = ?)
但类似查询
return $this->model->where('closed_at', null)
->with(['product:id,title', 'in_works' => function ($query) {
$query->where('closed_at', '=', null);
}])->get();
返回3个查询,为什么?
select * from `order` where `closed_at` is null
select `id`, `title` from `product` where `product`.`id` in (1, 2)
select * from `in_work` where `in_work`.`order_id` in (1, 2) and `closed_at` is null
答案 0 :(得分:1)
通过急切加载(即使用with
)添加到模型的每个关系都通过附加查询来查询。
这是您拥有的:
select * from `order` where `closed_at` is null -- Model
product:id,title
查询select `id`, `title` from `product` where `product`.`id` in (1, 2)
in_works
查询select * from `in_work` where `in_work`.`order_id` in (1, 2) and `closed_at` is null
请注意,在情况(2)和(3)中,(1,2)
对应于从第一个查询获得的order
的标识符。
whereHas
将进一步优化原始模型查询,因此会影响返回的总结果。对于您而言,以下是whereHas('users',...)
查询的结果:
select * from `order` where `closed_at` is null and exists (select * from `user` inner join `user_order` on `user`.`id` = `user_order`.`user_id` where `order`.`id` = `user_order`.`order_id` and `id` = ?)
当模型查询实际上未产生任何结果时,则实际上不执行任何关系查询。