laravel雄辩的orwhere问题

b0zn9rqh  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(404)

我一直在想我的问题到底出了什么问题,这应该很简单。更像是我在创建一个select*from表,其中(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'))

我希望它在第二个括号里是“and”

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

az31mfrm1#

如果你打电话 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
        ]);                
    });
});

相关问题