To use the Spatie Laravel Permission package effectively, several specific configurations and setup steps are required:
Installation and Publishing
- Install the package via Composer: `composer require spatie/laravel-permission`.
- Publish the migration and configuration file using the artisan command:
`php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"`.
- Run the migrations to create the necessary tables (`roles`, `permissions`, `model_has_permissions`, `model_has_roles`, `role_has_permissions`) in the database.
User Model Configuration
- The User model (or any authenticatable model you want to assign roles/permissions to) must use the `HasRoles` trait provided by the package. This enables role and permission management on the model.
Config File Adjustments
- The package publishes a `config/permission.php` file where you can customize settings such as table names, cache settings, and model classes.
- If you are using UUIDs instead of auto-incrementing IDs, you need to adjust the migrations and possibly the config file accordingly before running migrations.
- For multi-tenant or team-based setups, you must enable and configure the "teams" feature in the config file.
Cache Configuration
- The package uses caching to optimize permission lookups. By default, cache expiration is set to 24 hours.
- You can customize the cache store, prefix, and expiration time in `config/permission.php` under the `cache` array.
- In multi-tenant scenarios, it is advisable to set a unique cache prefix to avoid clashes.
- If your app switches tenants during a single request, you may need to manually reinitialize the permission cache.
Middleware
- To protect routes based on roles or permissions, register the package's middleware aliases (`role`, `permission`, `role_or_permission`) in your HTTP kernel or middleware configuration.
Advanced Customization
- You can extend the package's Role and Permission models if you want to customize behavior, but you must be careful to update the config file to point to your custom models.
- Changing the default database table names requires updating the config file and possibly the migrations.
- For UUID support or other advanced use cases, follow the package's advanced documentation sections.
In summary, the key configurations involve publishing and customizing the config file, adding the `HasRoles` trait to your user model, running migrations, configuring caching properly, and optionally setting up middleware and multi-tenant support. Following these steps ensures that the Spatie package will work effectively in your Laravel application[1][2][6][8][9].
Citations:
[1] https://spatie.be/docs/laravel-permission/v6/installation-laravel
[2] https://spatie.be/docs/laravel-permission/v6/prerequisites
[3] https://spatie.be/docs/laravel-data/v4/requirements
[4] https://packagist.org/packages/spatie/laravel-settings
[5] https://spatie.be/docs/laravel-html/v3/installation-setup
[6] https://www.honeybadger.io/blog/laravel-permissions-roles/
[7] https://github.com/spatie/laravel-slack-alerts/discussions/22
[8] https://github.com/spatie/laravel-permission/blob/main/docs/advanced-usage/cache.md
[9] https://www.allphptricks.com/laravel-11-spatie-user-roles-and-permissions/
[10] https://stackoverflow.com/questions/58114242/extending-the-spatie-role-model-but-use-a-different-database-table