Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception on seed database #126

Open
blob84 opened this issue Jul 8, 2020 · 5 comments
Open

Exception on seed database #126

blob84 opened this issue Jul 8, 2020 · 5 comments

Comments

@blob84
Copy link

blob84 commented Jul 8, 2020

Hello when I try to seed the db I get this error:

Spatie\Permission\Exceptions\PermissionDoesNotExist  : There is no permission named `edit listing` for guard `web.


  at /var/www/html/marketplacekit/vendor/spatie/laravel-permission/src/Exceptions/PermissionDoesNotExist.php:11
      class PermissionDoesNotExist extends InvalidArgumentException
      {
          public static function create(string $permissionName, string $guardName = '')
         {
          return new static("There is no permission named `{$permissionName}` for guard `{$guardName}`.");
        }
     
         public static function withId(int $permissionId)
         {

  Exception trace:

  1   Spatie\Permission\Exceptions\PermissionDoesNotExist::create("edit listing", "web")
      /var/www/html/marketplacekit/vendor/spatie/laravel-permission/src/Models/Permission.php:96

  2   Spatie\Permission\Models\Permission::findByName("edit listing", "web")
      /var/www/html/marketplacekit/vendor/spatie/laravel-permission/src/Traits/HasPermissions.php:327

how can I fix?

@lafree777
Copy link

Also a problem with
php artisan db:seed - Exception on seed database

@zacharyk88
Copy link

Having this same issue

@MPSimon
Copy link

MPSimon commented Aug 4, 2020

try:

1 - php artisan migrate:rollback
2- php artisan lada-cache:disable
3 -php artisan migrate
4- php artisan db:seed

Then you can login in the browser as admin with the defaults credentials:

user:admin@example.com
password:changeme

@thanphl
Copy link

thanphl commented Dec 12, 2020

I had this error while installing marketplacekit as well, this is how I resolved it:

The issue happens in database/seeds/RolesAndPermissionsSeeder.php when running at:
<>->givePermissionTo(<>)

Stacktrace:

[2020-12-11 10:35:59] production.ERROR: There is no permission named `edit listing` for guard `web`. {"exception":"[object] (Spatie\\Permission\\Exceptions\\PermissionDoesNotExist(code: 0): There is no permission named `edit listing` for guard `web`. at /media/thanphl/Working/workspace/marketplacekit/vendor/spatie/laravel-permission/src/Exceptions/PermissionDoesNotExist.php:11)
[stacktrace]
#0 /media/thanphl/Working/workspace/marketplacekit/vendor/spatie/laravel-permission/src/Models/Permission.php(96): Spatie\\Permission\\Exceptions\\PermissionDoesNotExist::create()
#1 /media/thanphl/Working/workspace/marketplacekit/vendor/spatie/laravel-permission/src/Traits/HasPermissions.php(327): Spatie\\Permission\\Models\\Permission::findByName()
#2 /media/thanphl/Working/workspace/marketplacekit/vendor/spatie/laravel-permission/src/Traits/HasPermissions.php(265): Spatie\\Permission\\Models\\Role->getStoredPermission()
#3 [internal function]: Spatie\\Permission\\Models\\Role->Spatie\\Permission\\Traits\\{closure}()
#4 /media/thanphl/Working/workspace/marketplacekit/vendor/laravel/framework/src/Illuminate/Support/Collection.php(932): array_map()
#5 /media/thanphl/Working/workspace/marketplacekit/vendor/spatie/laravel-permission/src/Traits/HasPermissions.php(266): Illuminate\\Support\\Collection->map()
#6 /media/thanphl/Working/workspace/marketplacekit/database/seeds/RolesAndPermissionsSeeder.php(33): Spatie\\Permission\\Models\\Role->givePermissionTo()
#7 [internal function]: RolesAndPermissionsSeeder->run()

The seeder already had clear cache key, so you do not need to run it again, but free to run so
image

Fix:

  • Open vendor/spatie/laravel-permission/src/Models/Permission.php
  • Update method public static function findByName(string $name, $guardName = null)
  • Replace:
$permission = static::getPermissions()->filter(function ($permission) use ($name, $guardName) {
            return $permission->name === $name && $permission->guard_name === $guardName;
        })->first();

with

$permission = Permission::where('name', $name)->where('guard_name', $guardName)->first();

Final code

image

  • Change assign admin role here as well since method assignRole has same issue with givePermissionTo, it can run with id of role instead of string value
    image

Hope this could help

@antonioconselheiro
Copy link

Nice thanphl, it solved for me, I had the same problem here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants