Reputation: 1136
How would I get all users that share one or more groups of a certain user?
I already have a many-to-many relationship between users and groups with the intermediate table group_user...
I thought using something like this pseudocode:
$users = User::with('groups')->whereIn('groups', $current_user->groups->all())->all();
Do I need to use hasManyThrough
somehow?
Upvotes: 0
Views: 213
Reputation: 5332
Assuming that the group_user
table has the primary key id
, probably something like below would work.
$users = User::whereHas('groups', function ($q) use ($current_user) {
$q->whereIn('id', $current_user->groups->pluck('id')->all());
})->get();
Upvotes: 2