My tables
- users
id | name |
---|---|
Cell 1 | Cell 2 |
Cell 3 | Cell 4 |
- group
id | name |
---|---|
Cell 1 | Cell 2 |
Cell 3 | Cell 4 |
- group_users
id | user_id | group_id |
---|---|---|
Cell 1 | Cell 2 | Cell 4 |
Cell 3 | Cell 4 | Cell 6 |
// All users which are members of group
public function users()
{
return $this->belongsToMany(User::class);
}
// All groups user belong to
public function groups()
{
return $this->belongsToMany(Group::class);
}
This is what I have tried to do. I think the problem is that I have to make the $users an array of ids that were fetched and I’m unable to do that. Please help
public function show(Group $group)
{
//Fetching all members of the group
$users = $group->users()->get()
return Inertia::render('Clients/Show', [
'users' => Group::whereNotIn('id', $users)->get()
]);
}
2
Answers
If you want to select users who don’t have a relation, you can use whereDoesntHave:
In this case I’m querying for all users who dont have a group with id 1 linked.