Laravel Eloquent Query with AND inside orWhere()

时间:2017-12-17 21:12:52

标签: php laravel laravel-5

$query->orWhere(['member_id'=> $participant['id'], "event_id"=>$event_id, 'member_status'=>'1'])

在使用toSql()调试Above查询时,我找到了这个

select * from group_members where group_members.`event_id` = 5 and group_members.`event_id` is not null and (member_id = ? and event_id = ? and member_status = ?) or (member_id = ? or event_id = ? or member_status = ?)

但是我希望它像,

select * from group_members where group_members.`event_id` = 5 and group_members.`event_id` is not null and (member_id = ? and event_id = ? and member_status = ?) or (member_id = ? and event_id = ? and member_status = ?)

我如何实现这一目标?

提前致谢

1 个答案:

答案 0 :(得分:3)

试试这个:

$query->where(function($query) {
    $query->where(['member_id'=> $participant['id'], "event_id"=>$event_id, 'member_status'=>'1']);
})->orWhere(function($query) {
    $query->where(['member_id'=> $participant['id'], "event_id"=>$event_id, 'member_status'=>'1']);
});