[英]Multiple whereHas Laravel eloquent query
Please help me to simplify Laravel eloquent query.请帮助我简化 Laravel 雄辩的查询。 Is there a way to make it more simple, please suggest because I have a couple of these in my code or suggest me any strategy to make it simpler?
有没有办法让它更简单,请提出建议,因为我的代码中有几个这样的方法,或者建议我任何使它更简单的策略?
if (!empty($has_due_date)){
$resourceQuery->whereHas('stages', function ($query) use ($has_due_date) {
$query->whereHas('groups', function ($query) use ($has_due_date) {
$query->whereHas('tasks', function ($query) use ($has_due_date){
if ($has_duedate == 'yes'){
$query->whereNotNull('due_date');
}else if($has_duedate == 'no'){
$query->whereNull('due_date');
}
});
});
});
}
The method whereNull has a third parameter not , so it could be just like this: whereNull方法有第三个参数not ,所以它可能是这样的:
$resourceQuery->when($has_due_date, function ($query, $hasDueDate) {
return $query->whereHas('stages.groups.tasks', function ($query) use ($hasDueDate) {
$query->whereNull('due_date', 'and', $hasDueDate === 'yes');
});
});
Intead of if
use conditional clause .代替
if
使用条件子句。
And don't forget you may use "dot" notation to execute a query against a nested relationship, more info here .并且不要忘记您可以使用“点”表示法对嵌套关系执行查询,更多信息请点击此处。
Maybe this:也许这个:
$resourceQuery->when(!empty($has_due_date), function ($q1) use ($has_due_date){
$q1->whereHas('stages.groups.tasks', function ($q2) use ($has_due_date) {
if ($has_due_date == 'yes'){
$q2->whereNotNull('due_date');
}else{
$q2->whereNull('due_date');
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.