TomasAm
6 months ago
615
1
Laravel

Get roles which does not belong to a specific user id

Posted 6 months ago by TomasAm

I am trying to foreach in blade multiselect all roles related to the user, this is easy to do due to relation, but I want also to foreach the rest not related roles to the user.

I try to use this controller:

$roles = Role::doesntHave('users')->get();

but it returns only users which are not linked with any roles at all, so how do I specify that it should return roles which are not related to a specific user which id comes with the controller request

 public function edit($id) {
 $roles = Role::doesntHave('users')->get();
....
}

users and roles are linked via pivot table many to many relation.

Please sign in or create an account to participate in this conversation.