I want to create a chat system on which I could list all the chats between two specific people
I have 2 tables users
andchats
There are 3 columns - user_id
, friend_id
andchat
My model file is User.php
as follows
public function chats() {
return $this->hasMany('App\Chat');
}
For instance,
I want to list the entire chat between user 1 and 3 without changing the order of the conversation.
I can just do this by completing $chats = Auth::user()->chats->where('friend_id', '=', $id);
, but this will only give authenticated users (who are users 1 or 3). But I want to talk between them.
So I found an alternative way to do this with
$first = Chat::all()->where('user_id', '=', Auth::user()->id)->where('friend_id', '=', $id);
$second = Chat::all()->where('user_id', '=', $id)->where('friend_id', '=', Auth::user()->id);
$chats = $first->merge($second);
. . , .
, , ?
, .