检查用户是否在聊天室 Laravel

Check user if is in chat room Laravel

我有模特:

聊天室成员
聊天室
所以我想检查授权用户是否在聊天室

我的人际关系:

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);
    }

在用户模型中添加以下关系

public function chatRoomMember(){

   return $this->hasMany(ChatRoomMembers::class);
}

在代码中

auth()->user()->chatRoomMember->count()

auth()->user()->chatRoomMember->exists()

所以我在用户模型中创建了关系:

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

我只得到这个用户的聊天室