Laravel-Permission
Roles and permissions are an important part of many web applications. Laravel historically had a lot of packages for them and improved the core code as well. So what is the situation in this market today? What packages are the best to use? I’ve picked Spatie.
Installation in Laravel
- Consult the Prerequisites page for important considerations regarding your User models!
- This package publishes a
config/permission.php
file. If you already have a file by that name, you must rename or remove it. - You can install the package via composer:
composer require spatie/laravel-permission
- Optional: The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:
'providers' => [
// ...
Spatie\Permission\PermissionServiceProvider::class,
];
- You should publish the migration and the config/permission.php config file with:
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider
- NOTE: If you are using UUIDs, see the Advanced section of the docs on UUID steps, before you continue. It explains some changes you may want to make to the migrations and config file before continuing. It also mentions important considerations after extending this package’s models for UUID capability.
- Clear your config cache. This package requires access to the permission config. Generally, it’s bad practice to do config-caching in a development environment. If you’ve been caching configurations locally, clear your config cache with either of these commands:
php artisan optimize:clear
#or
php artisan config:clear
Run the migrations: After the config and migration have been published and configured, you can create the tables for this package by running:
php artisan migrate
🎁 Bonus Gift
You can use it as a boilerplate to manage roles and permissions.