I am building a simple private chat app with laravel.
I have a messages table with a column : id/sender_id/receiver_id and body
I am trying to show chat history related to user when i select to any user but unfortunately my query is not working properly all users seeing all message please help me how can i resolved that thank u.
Controller
Message::where('sender_id', Auth::user()->id)
->orWhere('receiver_id', Auth::user()->id)
->where('sender_id', $userId)
->orWhere('receiver_id', $userId)
->get();
CodePudding user response:
make subquery where like this:
Message::where(function($q){
$q->where('sender_id', Auth::user()->id)
->orWhere('receiver_id', Auth::user()->id);
})->where(function($q){
$q->where('sender_id', $userId)
->orWhere('receiver_id', $userId)
})->get();
CodePudding user response:
Just add ->where(function($q){})
case orWhere breakdown all other conditions
try this code
Message::where(function ($q) {
$q->where('sender_id', Auth::user()->id)
->orWhere('receiver_id', Auth::user()->id);
})->OrWhere(function ($q) {
$q->where('sender_id', $userId)
->orWhere('receiver_id', $userId);
})->get();