我正在将Laravel与spatie / laravel-permission包一起使用,如本tutorial中所述。
但是,当我尝试创建虚拟数据时,它返回以下错误 Spatie \ Permission \ Exceptions \ RoleDoesNotExist:没有名为Admin
的角色。
下面是生成伪数据的类。
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Ask for db migration refresh, default is no
if ($this->command->confirm('Do you wish to refresh migration before seeding, it will clear all old data ?')) {
// Call the php artisan migrate:refresh
$this->command->call('migrate:refresh');
$this->command->warn("Data cleared, starting from blank database.");
}
// Seed the default permissions
Permission::firstOrCreate(['name' => 'Administer roles & permissions', 'guard_name' => 'isAdmin']);
$this->command->info('Default Permissions added.');
// Confirm roles needed
if ($this->command->confirm('Create Roles for user, default is admin and user? [y|N]', true)) {
// Ask for roles from input
$input_roles = $this->command->ask('Enter roles in comma separate format.', 'Admin,User');
// Explode roles
$roles_array = explode(',', $input_roles);
// add roles
foreach($roles_array as $role) {
$role = Role::firstOrCreate(['name' => trim($role), 'guard_name' => 'isAdmin']);
if( $role->name == 'Admin' ) {
// assign all permissions
$role->syncPermissions(Permission::all());
$this->command->info('Admin granted all the permissions');
} else {
// for others by default only read access
$role->syncPermissions(Permission::where('name', 'LIKE', 'view_%')->get());
}
// create one user for each role
$this->createUser($role);
}
$this->command->info('Roles ' . $input_roles . ' added successfully');
} else {
Role::firstOrCreate(['name' => 'User']);
$this->command->info('Added only default user role.');
}
// now lets seed some posts for demo
$this->command->info('Some Posts data seeded.');
$this->command->warn('All done :)');
}
/**
* Create a user with given role
*
* @param $role
*/
private function createUser($role)
{
$user = factory(User::class)->create();
$user->assignRole($role->name);
if( $role->name == 'Admin' ) {
$this->command->info('Here is your admin details to login:');
$this->command->warn($user->email);
$this->command->warn('Password is "secret"');
}
}
}
有解决的主意吗?
答案 0 :(得分:0)
您需要运行
php artisan cache:forget spatie.permission.cache
使用角色或权限之前
答案 1 :(得分:0)
我通过更改config / auth.php文件中的警卫顺序来解决此错误:
这是默认设置
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
这就是我现在拥有的
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
'web' => [
'driver' => 'session',
'provider' => 'users',
],
],
我发现,当我将默认防护设置更改为“ api” (默认设置为“ web” )时,出现了此错误。
当您为角色播种而不更改顺序时,它会正确将guard_name设置为“ api” ,但是当它想将这些角色应用于用户时,可能会使用“ web” 后卫。
如果您使用的是默认值守卫,但仍然出现此错误,只需尝试在这样的角色播种器中设置guard_name字段
Role::create(['name' => 'admin', 'guard_name' => 'web']);
答案 2 :(得分:0)
由Spatie / Laravel-permission引起的此问题还将模型类型存储在db中,并将许可权授予BackpackUser或User模型。
请参阅此链接以供参考:
https://github.com/Laravel-Backpack/PermissionManager/issues/193]
该解决方案刚刚于2019年底从烟熏炉中添加:
https://backpackforlaravel.com/docs/4.0/base-about#having-both-regular-users-and-admins
基本上,您需要添加中间件,或者在用户上添加is_admin列,或者使用tabacitu提供的中间件