有条件的地方基于另一个模型的价值 [英] conditional where on the basis of value of another model
问题描述
我有三种型号
发票
工作室
R_studio_pay
关系如下
工作室有很多发票
工作室有一个 R_studio_pay
studio hasOne R_studio_pay
如果R_studio_pay中的is_r列为1,我需要获取发票-> where('recurring','single')-> get(),否则我不希望where子句.
I need to get invoices->where('recurring', 'single')->get() if a column is_r is 1 in R_studio_pay else i don't want the where clause.
我尝试使用whereHas studio->在哪里R_studio_pay但有条件的地方无法完成.
I tried using whereHas studio-> wherehas R_studio_pay but conditional where cannot be done.
$invoices = invoice::with('studio')->whereHas('studio', function($query) {
$query->whereHas('r_studio_pay', function($query) {
$query->where('is_r', 1);
});
})->where('recurring', 'single')
但是不能在有条件的地方应用.
But couldn't apply conditional where.
推荐答案
您处在正确的轨道上.但是,您应该在回调中添加return语句. return $query->where('is_r', 1);
代替$query->where('is_r', 1);
.尝试下面的代码.
You are on the right track. However, you should add a return statement in your callbacks. return $query->where('is_r', 1);
instead of $query->where('is_r', 1);
. Try the code below.
$invoices = invoice::with('studio')->whereHas('studio', function($query){
return $query->whereHas('r_studio_pay', function($query){
return $query->where('is_r', 1);
});
})->where('recurring', 'single');
这篇关于有条件的地方基于另一个模型的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!