lundi 22 juillet 2019

Laravel Backpack Permission seeding "role does not exist"

I would like to ask about Seeding in Laravel BackPack Permission Manager,

I'm trying default Backpack Permission Manager configuration, and trying to seed it with dummy data.

Here is my Roles and Permission seeder method:

    public function run()
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // create permissions
        Permission::create(['name' => 'view members']);
        Permission::create(['name' => 'add members']);
        Permission::create(['name' => 'edit members']);
        Permission::create(['name' => 'delete members']);

        Role::create(['name' => 'super-admin'])
            ->givePermissionTo(Permission::all());

        Role::create(['name' => 'admin'])
            ->givePermissionTo('add members');
    }

And here is my User seeder method:

    public function run()
    {
        factory(User::class)->create([
            'name' => 'Arianna',
            'email' => 'arianna@example.com'
        ])->assignRole('super-admin');
    }

These Seeder is fine, but I realized that the Users CRUD interface doesn't show roles correctly

users roles shown as "-",

roles shown as -

and when I checked the database, model_has_roles shown App\User as model type

App\User model_type

I'm sure model_type should be App\Models\BackpackUser , so I tried seeding again now with BackpackUser class

now I got different problem, BackpackUser don't have any roles, as roles needs to be in "backpack" guard to be assigned to BackpackUser

 Spatie\Permission\Exceptions\RoleDoesNotExist  : There is no role named `super-admin`.

  at .\vendor\spatie\laravel-permission\src\Exceptions\RoleDoesNotExist.php:11
     7| class RoleDoesNotExist extends InvalidArgumentException
     8| {
     9|     public static function named(string $roleName)
    10|     {
  > 11|         return new static("There is no role named `{$roleName}`.");
    12|     }
    13|
    14|     public static function withId(int $roleId)
    15|     {

  Exception trace:

  1   Spatie\Permission\Exceptions\RoleDoesNotExist::named("super-admin")
      .\vendor\spatie\laravel-permission\src\Models\Role.php:91

  2   Spatie\Permission\Models\Role::findByName("super-admin", "backpack")
      .\vendor\spatie\laravel-permission\src\Traits\HasRoles.php:270


what is my options? Why I can't just seed normally with default configuration?

please don't say that I need to manually "seed" the dummy data using Users interface..



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire