It sounds more like a many to many relationship, than a one to many? https://laravel.com/docs/9.x/eloquent-relationships#many-to-many
Don't you have a pivot table?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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 ?
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();
Please or to participate in this conversation.