Factories/Seeders
The package includes factories for all three models (e.g. User::factory(), and the template includes seeders:
database/seeders/UserSeeder.phpdatabase/seeders/RoleSeeder.phpdatabase/seeders/PermissionSeeder.php
Seeding Users
The user factory has the following helper methods:
->active()- Ensures the generated user can log in->withPermission(string $permission)- Gives the user a single permission->withPermissions(array $permission)- Gives the user multiple permissions
These can be used in test cases - for example:
$user = User::factory()
->active()
->withPermission(Permission::EditPost)
->create();
There isn't currently a helper for roles, but you can call the underlying Spatie class method like this:
$user = User::factory()
->active()
->create()
->assignRole('Developer');
There is an override for the UserFactory in the template (database/factories/UserFactory.php) in case you need to add extra fields (override definition() and/or add extra state methods).
The default developer login is test@example.com/password. Make sure you change it on the live/staging sites (manually in the admin area - don't put the password in the code).
Seeding Roles
By default, a "Developer" role is created for us to use - you should generally give this role all permissions, but we don't do that automatically because it isn't always appropriate.
Please create additional roles for clients to use (e.g. "Administrator"), and only give them the permissions they require (both for security and to keep the GUI simple for them).
Seeding Permissions
The permission seeder just calls permission:sync. It doesn't normally need to be modified, but if you wanted to add something to the Notes field you could do this:
Artisan::call('permission:sync', ['--force' => true]);
Permission::findByName(PermissionEnum::CreateExamples)
->update(['notes' => 'Lorem ipsum dolor sit amet']);