Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FounderStartup's avatar

An eloquent query with 3 models.....

A User can join any number of INNER CIRCLES ( something like groups ). Now whenever a User list a property , I need to send emails to all the members of all the circles he has joined. What will be the query ?

Model User


    public function circlemember()
    {
        return $this->hasMany(InnerCircleMember::class, 'member_id', 'id');
    }

Model InnerCirclemember

     public function member(){
        return $this->belongsTo(User::class,'member_id');
    }


    public function circle(){
        return $this->belongsTo(InnerCircle::class,'circle_id');
    }

Model InnerCircle

    public function circlemember()
    {
        return $this->hasMany(InnerCircleMember::class, 'circle_id', 'id');
    }

Controller:

     $allusers = User::where('status' , 1)
                ->whereRoleIs('broker')
                ->where('email_verified_at', '<>', NULL)
                ->where('mailforcircles', 1)
                ->get();

What will be the correct query ? Presently I have no idea from where I should start ? :)

0 likes
2 replies
FounderStartup's avatar

@MichalOravec There is a huge difference. The thread you mentioned has a simple one to many relationship query. But in this query , I need a different collection. Let me clarify again :

A User can join any number of INNER CIRCLES ( something like group ). Now whenever a User list a property , I need to send emails to all the members of all the circles he has joined. What will be the query ?

Here a user may have joined multiple inner circles and we need to find all the users who are members of all the circles he is member of. Kindly help if it is possible for you :)

Please or to participate in this conversation.