-
Hello hello! 👋 This is the first time using This is the code in my <?php
declare(strict_types=1);
namespace Database\Seeders;
use App\Models\Material;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
final class DatabaseSeeder extends Seeder
{
use WithoutModelEvents;
/**
* Seed the application's database.
*/
public function run(): void
{
$this->call([
RolesAndPermissionsSeeder::class,
]);
// User::factory(10)->create();
User::factory()->create([
'name' => 'Test User',
'email' => '[email protected]',
]);
// Material
Material::factory(3)->create();
}
} The In this situation when I run the command [...]
INFO Seeding database.
Database\Seeders\RolesAndPermissionsSeeder ............................................................................................... RUNNING
Spatie\Permission\Exceptions\PermissionDoesNotExist
There is no permission named `edit articles` for guard `web`.
at vendor/spatie/laravel-permission/src/Exceptions/PermissionDoesNotExist.php:11
7▕ class PermissionDoesNotExist extends InvalidArgumentException
8▕ {
9▕ public static function create(string $permissionName, ?string $guardName)
10▕ {
➜ 11▕ return new static("There is no permission named `{$permissionName}` for guard `{$guardName}`.");
12▕ }
13▕
14▕ /**
15▕ * @param int|string $permissionId
+6 vendor frames
7 database/seeders/RolesAndPermissionsSeeder.php:26
Spatie\Permission\Models\Role::givePermissionTo()
+8 vendor frames
16 database/seeders/DatabaseSeeder.php:22
Illuminate\Database\Seeder::call() While trying to understand the issue I made some tests. I noticed the following behavior: If I store the first <?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class RolesAndPermissionsSeeder extends Seeder
{
public function run(): void
{
// Reset cached roles and permissions
app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
// create permissions
$permission1 = Permission::create(['name' => 'edit articles']); // PERMISSION STORED IN VARIABLE
Permission::create(['name' => 'delete articles']);
Permission::create(['name' => 'publish articles']);
Permission::create(['name' => 'unpublish articles']);
// create roles and assign created permissions
// this can be done as separate statements
$role = Role::create(['name' => 'writer']);
$role->givePermissionTo($permission1); // VARIABLE USED TO LINK PERMISSION TO ROLE
// or may be done by chaining
$role = Role::create(['name' => 'moderator'])
->givePermissionTo(['publish articles', 'unpublish articles']);
$role = Role::create(['name' => 'super-admin']);
$role->givePermissionTo(Permission::all());
}
} Any clarification or explanation of this behavior would be enormously helpful? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The examples don't specify a guard (because they're intended to be more generic), so it defaults to the first guard defined in your But, as you've done, assigning the created record to a variable, and using that later is a perfectly acceptable approach. |
Beta Was this translation helpful? Give feedback.
OH ... Maybe you're using
use WithoutModelEvents;
?Try without that.
Or add a cache-flush after creating those permissions:
use WithoutModelEvents; public function run(): void { // Reset cached roles and permissions app()[PermissionRegistrar::class]->forgetCachedPermissions(); // create permissions Permission::create(['name' => 'edit articles']); Permission::create(['name' => 'delete articles']); Permission::create(['name' => 'publish articles']); Permission::create(['name' => 'unpublish articles']); // update cache + app()[PermissionRegistrar::class]->forgetCachedPermissions();