detachRole method

Future<bool> detachRole(
  1. RoleType role, {
  2. DatabaseDisk disk = Model.defaultDisk,
})

Detaches the given role from the user.

If the role record exists and the user currently has it, the role is removed through the user/role pivot table.

Returns true when the role is detached successfully.

Example:

await user.detachRole(RoleType.guest);

Implementation

Future<bool> detachRole(RoleType role, {DatabaseDisk disk = Model.defaultDisk}) async {
  final roleRecord = await Model.firstWhere<Role>(field: 'name', value: role.name);
  if (roleRecord != null) {
    if (await hasRole(role)) {
      return detach(roleRecord, relationship: .belongsToMany, table: UserRolePivotTable(), disk: disk);
    }
    return false;
  }
  return false;
}