Hello,
My code works correctly when like this,
$user->removeRole($user->roles->first());
But I am trying remove multiple roles like this;
$user->removeRole($user->roles);
and Laravel throwing this error message '
Return value of AppUser::getStoredRole() must implement interface SpatiePermissionContractsRole, instance of IlluminateDatabaseEloquentCollection returned'
isnt there any collection support for remove role action ?
isnt there any collection support for remove role action ?
No, there are no plans to support passing a collection to removeRole.
There are 3 alternatives:
Remove roles 1-by-1 as you're doing.
Given that in most cases of removing "all" roles you probably are removing in order to assign different roles, you could use syncRoles to remove all and attach a new set and still reset the cache appropriately all in one command:
https://github.com/spatie/laravel-permission/blob/e3821559c69b2b1ea8fdd967fa89ba13a01ead78/src/Traits/HasRoles.php#L142-L170
You could just manually call $user->roles()->detach() and then either assign a new role immediately so the cache gets updated automatically or manually call $user-> forgetCachedPermissions().
Most helpful comment
No, there are no plans to support passing a collection to
removeRole.There are 3 alternatives:
Remove roles 1-by-1 as you're doing.
Given that in most cases of removing "all" roles you probably are removing in order to assign different roles, you could use
syncRolesto remove all and attach a new set and still reset the cache appropriately all in one command:https://github.com/spatie/laravel-permission/blob/e3821559c69b2b1ea8fdd967fa89ba13a01ead78/src/Traits/HasRoles.php#L142-L170
You could just manually call
$user->roles()->detach()and then either assign a new role immediately so the cache gets updated automatically or manually call$user-> forgetCachedPermissions().