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

mhasan's avatar

Many to many relationship between Roles and permissions

Hello experts,

How do i query permissions that are not aleady assigned in a role. The intermideary table has permission_id, and role_id fields, table name: permission_role. Help much appreciated.

0 likes
2 replies
rdelorier's avatar

You can use a where in for this

Permissions::whereNotIn('id',$role->permissions->lists('id'));
1 like
mhasan's avatar
mhasan
OP
Best Answer
Level 9

Thank for the suggestion. It works!! Only problem with your submitted code is Permissions, where it should be Permission. Updated code: public function getUnAssignedPermissions($id){ $role = Role::find($id);

    $permissions = Permission::whereNotIn('id',$role->permissions->lists('id', 'name'))->get();
    return $permissions;

}

Please or to participate in this conversation.