Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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);
    }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

In user model add below relationship

public function chatRoomMember(){

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

and in code

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

or

auth()->user()->chatRoomMember->exists()
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!