Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the steps to create and seed roles and permissions using Spatie's package


What are the steps to create and seed roles and permissions using Spatie's package


To create and seed roles and permissions using Spatie's Laravel Permission package, follow these detailed steps:

1. Install and Set Up the Package

- Install the package via Composer:


  composer require spatie/laravel-permission
  

- Publish the configuration and migration files:

  php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
  

- Run the migrations to create the necessary tables (`roles`, `permissions`, `model_has_roles`, etc.):

  php artisan migrate
  

2. Create a Seeder for Roles and Permissions

- Generate a seeder class:


  php artisan make:seeder RolesAndPermissionsSeeder
  

- In the seeder file (e.g., `database/seeders/RolesAndPermissionsSeeder.php`), include the Spatie models at the top:
php
  use Spatie\Permission\Models\Role;
  use Spatie\Permission\Models\Permission;
  use Spatie\Permission\PermissionRegistrar;
  

- Clear the cached permissions to avoid stale data issues:
php
  app()[PermissionRegistrar::class]->forgetCachedPermissions();
  

- Create permissions using the `Permission::create()` method:
php
  Permission::create(['name' => 'edit articles']);
  Permission::create(['name' => 'delete articles']);
  Permission::create(['name' => 'publish articles']);
  Permission::create(['name' => 'unpublish articles']);
  

- Create roles and assign permissions to them:
php
  $writer = Role::create(['name' => 'writer']);
  $writer->givePermissionTo('edit articles');

  $moderator = Role::create(['name' => 'moderator']);
  $moderator->givePermissionTo(['publish articles', 'unpublish articles']);
  

- Optionally, assign roles to users by retrieving the user model and calling `assignRole()`:
php
  $user = User::find(1); // assuming user with ID 1 exists
  $user->assignRole('writer');
  

3. Run the Seeder

- Add the seeder class to `DatabaseSeeder.php` or run it directly:


  php artisan db:seed --class=RolesAndPermissionsSeeder
  

Additional Notes:

- Always clear the permission cache before seeding roles and permissions to ensure changes take effect immediately.
- Roles act as collections of permissions; you assign permissions to roles and then assign roles to users.
- You can also assign permissions directly to users if needed.
- This seeding approach is straightforward and useful for initial setup or testing, but for production, you might consider a UI for managing roles and permissions dynamically.

This process is aligned with the official Spatie documentation and common Laravel practices for seeding roles and permissions[1][4][6][8][9].

Citations:
[1] https://spatie.be/docs/laravel-permission/v6/introduction
[2] https://www.fundaofwebit.com/post/laravel-10-spatie-user-roles-and-permissions-tutorial
[3] https://www.youtube.com/watch?v=C4PVTSkLf14
[4] https://spatie.be/docs/laravel-permission/v6/basic-usage/role-permissions
[5] https://itsolutionstuff.com/post/laravel-8-user-roles-and-permissions-tutorialexample.html
[6] https://www.youtube.com/watch?v=3hSBJCVwh78
[7] https://www.interviewsolutionshub.com/blog/complete-guide-to-spatie-role-and-permission-in-laravel-11
[8] https://www.honeybadger.io/blog/laravel-permissions-roles/
[9] https://spatie.be/docs/laravel-permission/v6/advanced-usage/seeding
[10] https://stackoverflow.com/questions/51434687/spatie-laravel-permissions-dummy-data-error
[11] https://www.reddit.com/r/laravel/comments/1e78vct/strategy_for_permission_and_authorisation_design/