Laravel和Spatie / laravel权限

时间:2018-10-10 09:12:16

标签: laravel spatie

从用户中删除所有角色的最佳或通常方法是什么?

我尝试过

$roles = $user->getRoleNames(); $user->removeRole($roles);

Return value of App\User::getStoredRole() must implement interface Spatie\Permission\Contracts\Role, instance of Illuminate\Support\Collection returned

预先感谢

6 个答案:

答案 0 :(得分:2)

使用普通的Laravel detach方法,如下所示:

$user->roles()->detach();

答案 1 :(得分:1)

从阅读文档可以清楚地看出,您可以将AbstractList$Itr实例传递给 public E next() { checkForComodification(); // will throw ConcurrentModificationException // before the Iterator's state is changed try { int i = cursor; E next = get(i); lastRet = i; cursor = i + 1; return next; } catch (IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } } ,所以我认为您做对了。

  

assignRole,hasRole,hasAnyRole,hasAllRoles和removeRole函数可以接受字符串,\ Spatie \ Permission \ Models \ Role对象或\ Illuminate \ Support \ Collection对象。

答案 2 :(得分:1)

我现在以这种方式$user->removeRole($user->roles->first());

答案 3 :(得分:0)

您还可以通过同步到一个空数组来删除所有角色。

$user->syncRoles([]);

我确认它有效。

答案 4 :(得分:0)

即使在Laravel 7上也可以正常工作

对于角色: words[i-1]

对于权限: $user->syncRoles([]);

答案 5 :(得分:0)

在Spatie文档中,您可以找到一种删除所有先前角色并简单分配新角色的方法

$user->syncRoles($roles);

作为参考,您可以访问此link

相关问题