Skip to content
Advertisement

Check user if is in chat room Laravel

I have models:

ChatRoomMembers
ChatRoom
so I want to check if auth user is in chat room

my relationships:

ChatRoom:

    public function chatRoomMembers()
    {
        return $this->hasMany(ChatRoomMember::class);
    }

ChatRoomMembers:

    public function chatRoom()
    {
        return $this->belongsTo(ChatRoom::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

Advertisement

Answer

So I created relationship in User Model:

    public function chatRooms(){
        return $this->hasManyThrough(
           ChatRoom::class,
           ChatRoomMember::class,
           'user_id',
           'id',
           'id',
           'chat_room_id'
        )->orderBy('created_at', 'DESC');
    }

and I am getting only this user chat rooms

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement