Search code examples
phplaravel-5laravel-backpacklaravel-permission

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 -

enter image description here

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

App\User model_type:

enter image description here

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..


Solution

  • This issue caused by Spatie/Laravel-permission also stores model type in the db and puts the permission on the BackpackUser or User model.

    Well.. just recently got answer from here:

    https://github.com/Laravel-Backpack/PermissionManager/issues/193]

    the solution is just added on end of 2019 from tabacitu:

    https://backpackforlaravel.com/docs/4.0/base-about#having-both-regular-users-and-admins

    basically you need either to add middleware, either add is_admin column on User or use the middleware provided by tabacitu