我有一个像下面的一个在User
模型的模型关系,添加“orWhere”到laravel雄辩模型关系
public function notifications(){
return $this->morphMany('App\Notification','receiver')
->orWhere('type', 2);
}
这种运作良好,如果没有其他条件。但是,如果$user
是User
一个实例,并且我叫喜欢,$user->notifications->where('status', 1);
,
查询条件为,
SELECT * FROM
notifications
其中notifications
。receiver_id
= 3 和notifications
。receiver_id
不为空并且notifications
。receiver_type
=用户或type
= 2和status
= 1
这里的问题是,最终的状态是工作为OR
因为我无法组的关系的条件。因此,即使基础关系条件失败,但只有status & type
字段匹配,它也会获取该记录。
我需要的是在关联条件之后的第一个where和close之后添加括号,以便任何其他应用条件的地方都不会影响基本查询。
我需要的:(。notifications
receiver_id
= 3 和notifications
receiver_id
不为空并且 notifications
receiver_type
=用户或type
= 2 )
SELECT * FROM
notifications
其中和status
= 1
谢谢!这工作,我知道参数分组。我的疑问是将morphMany包含在组中,并正在寻找解决方案,如@ kapil.dev的答案。不幸的是,这并没有奏效。 –