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

FounderStartup's avatar

What will be the eloquent query with three relations ?

I am developing a real estate site. 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 ?

User model :


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

InnerCircleMember model

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


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

InnerCircle model :

    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 query ? Can anybody share any link to any such an example ? Or exactly how I should proceed ?

0 likes
19 replies
Sinnbeck's avatar

@FounderStartup so each user has a member_id on the users table? Or each circle as a member_id? Just trying to understand

1 like
FounderStartup's avatar

Let me explain.

User table : it has id as primary key.

Now there are three tables :

InnerCircles : This is a master table with unique id for every circle. InnerCircleMembers : This table has two fields : circle_id and member_id.

Sinnbeck's avatar

@FounderStartup ah InnerCircleMembers is a pivot table then. You just need to update your relationships to be belongs to many and tell it the name of the pivot table

Or you can just give it the proper name. inner_circle_user should be it I think. And on that table you would have user_id and inner_circle_id

1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You can then get all the ids of the inner circles like this

$ids = $user->innercircle()->pluck('id');

And use them to get a user list

$members = User::where('id', '<>', $user->id)->whereHas(function($query) use ($ids) {
    $query->whereIn('id', $ids);
})->get();
1 like
FounderStartup's avatar

@Sinnbeck Thanks chief. But it seems to be daunting task for me as I have never done it before :) Any help ? Or any such example ?

FounderStartup's avatar

@Sinnbeck No I don't have subscription. It seem I need to understand this eloquent relationships a bit deeper.

Sinnbeck's avatar

@FounderStartup well you seem close. You already came up with a pivot yourself. It's only about how you use it. I'm sure there are some free tutorials on YouTube as well

1 like
FounderStartup's avatar

@Sinnbeck OK. Thanks for your precious time. Will study a bit and will try to do it. Else you are always there for any help :).

kokoshneta's avatar

@FounderStartup You should read the section on many-to-many relationships that @sinnbeck linked to above, carefully. It explains everything very clearly.

If you do as suggested and rename your tables appropriately, then things become very easy for you. Name them this way:

  • users for the User model – the ID column should be called id
  • inner_circles for the InnerCircle model – the ID column should be called id
  • inner_circle_user for the pivot table – should have two columns called user_id and inner_circle_id (not member_id and circle_id)

If you do that, then you can simply do this in your models:

User model

public function innercircles() {
	return $this->belongsToMany(InnerCircle::class);
}

InnerCircler model

public function users() {
	return $this->belongsToMany(User::class);
}

That’s it!

Note that you don’t need the InnerCircleMember model at all, only the User and InnerCircle models are necessary. Laravel handles the rest.

So if you then need to retrieve all the inner circles that a user (let’s assume it’s the currently authenticated user) has joined and get all the other users of those circles so you can send them all e-mails, you simply do this in your controller:

$user = Auth::user();
$user->load('innercircles', 'innercircles.users');
$circleMembers = $user->innercircles->transform(
	fn($circle) => $circle->users->except([$user->id])
);

Updated to reflect that you wanted to load all the circles the user has joined, not just the one where the property was listed. Of course you’ll still have to do a bit of extra work to make sure that each user is only included once – you don’t want to send them a separate e-mail for every group they’re in. You can probably do that most easily by flattening the collection and getting the unique elements.

1 like
FounderStartup's avatar

@kokoshneta Thanks for your precious time bro. I will try your suggestions. But sorry I have already marked the best answer. So it will be a DUE on me :) Next time :)

kokoshneta's avatar

@FounderStartup I just realised you said you wanted to load all the circles the user has joined, not just the one where the property was listed, so I updated the answer.

1 like
Sinnbeck's avatar

@FounderStartup pick which ever answer you feel best helps you. @kokoshneta did a great job of explaining it all, and I honestly have no problem loosing a best answer to him :)

1 like

Please or to participate in this conversation.