Laravel雄辩的orWhere问题

时间:2018-07-03 01:00:38

标签: mysql laravel eloquent

一直徘徊我的查询出了什么问题应该看起来很简单。 它更像是我从表中创建一个选择*(a = 1和b = 2) 或(a = 2和b = 1)查询类型。这是我的雄辩。

return $query->where(function($q) use ($brand, $influencer, $agency){
    $q->where([
        'sender_id' => $brand,
        'receiver_id' => $influencer
    ]);
    $q->orWhere([
        'sender_id' => $influencer,
        'receiver_id' => $brand
    ]);
});

我的口才很好,但是在调试栏中显示时会显示

select count(*) as aggregate
from `chat_messages`
where ((`sender_id` = '415' and `receiver_id` = '1159')
or (`sender_id` = '1159' or `receiver_id` = '415'))

我希望它在第二个括号组内为“与”

select count(*) as aggregate
from `chat_messages`
where ((`sender_id` = '415' and `receiver_id` = '1159')
or (`sender_id` = '1159' AND `receiver_id` = '415'))

1 个答案:

答案 0 :(得分:2)

如果您使用数组调用orWhere(),则它在前面和各个约束之间使用OR

使用此:

return $query->where(function($q) use ($brand, $influencer, $agency){
    $q->where([
        'sender_id' => $brand,
        'receiver_id' => $influencer
    ])->orWhere(function($q) use ($brand, $influencer) {
        $q->where([
            'sender_id' => $influencer,
            'receiver_id' => $brand
        ]);                
    });
});