Reputation: 220
Been wandering what's wrong with my query should be as easy as it seems. its more like i'm creating a select * from table where (a = 1 and b =2) or (a = 2 and b = 1) type of query. Here's my eloquent.
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
]);
});
I have this query in my eloquent but when display in debug bar it's showing
select count(*) as aggregate
from `chat_messages`
where ((`sender_id` = '415' and `receiver_id` = '1159')
or (`sender_id` = '1159' or `receiver_id` = '415'))
and i wanted it to be "AND" inside the second parenthesis group
select count(*) as aggregate
from `chat_messages`
where ((`sender_id` = '415' and `receiver_id` = '1159')
or (`sender_id` = '1159' AND `receiver_id` = '415'))
Upvotes: 0
Views: 81
Reputation: 25936
If you call orWhere()
with an array, it uses OR
in front and between the individual constraints.
Use this:
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
]);
});
});
Upvotes: 2