To manually register the Spatie service provider in a Laravel application, follow these detailed steps:
1. Install the Spatie Package via Composer
Run the following command in your Laravel project directory to install the package:
composer require spatie/laravel-permission
Normally, the service provider is auto-registered by Laravel, but for manual registration, proceed to the next step[1][5][6].
2. Manually Add the Service Provider to the Configuration
Open the `config/app.php` file (for Laravel 10 or older) or `bootstrap/providers.php` (in newer Laravel versions if applicable).
Locate the `providers` array and add the following line to it:
php
Spatie\Permission\PermissionServiceProvider::class,
This explicitly registers the Spatie permission service provider with your application[1][5][6].
3. Publish the Package Configuration and Migration Files
To publish the configuration file (`config/permission.php`) and the migration files for roles and permissions tables, run:
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
This command copies the packageâs config and migration files into your Laravel project, allowing you to customize and migrate the database schema as needed[1][5][8].
4. Run Database Migrations
After publishing, run the migrations to create the necessary tables in your database:
php artisan migrate
This will create tables such as `permissions`, `roles`, `model_has_permissions`, `model_has_roles`, and `role_has_permissions`[5].
5. Clear Configuration Cache (If Applicable)
If you have cached your configuration files, clear the cache to ensure Laravel loads the new configuration properly:
php artisan config:clear
This step prevents issues related to stale config caching during development[5][8].
6. Additional Considerations
- If you are using UUIDs for your models or want to enable the "teams" feature, consult the Spatie documentation for advanced configuration before running migrations[1][8].
- Make sure your user model uses the appropriate traits provided by the package to enable roles and permissions functionality.
By following these steps, you manually register the Spatie service provider and set up the package correctly in your Laravel application.
Citations:
[1] https://github.com/spatie/laravel-permission/blob/main/docs/installation-laravel.md
[2] https://spatie.be/docs/laravel-html/v3/installation-setup
[3] https://laracasts.com/discuss/channels/laravel/how-to-manually-registering-package-components
[4] https://github.com/spatie/laravel-settings/discussions/140
[5] https://geekyants.com/en-us/blog/laravel-third-party-package---laravel-permission-spatie
[6] https://www.fundaofwebit.com/post/laravel-10-spatie-user-roles-and-permissions-tutorial
[7] https://laravel.com/docs/11.x/providers
[8] https://acquaintsoft.com/blog/laravel-permission-demo
[9] https://spatie.be/docs/laravel-activitylog/v4/installation-and-setup
[10] https://www.allphptricks.com/simple-laravel-10-user-roles-and-permissions/
[11] https://spatie.be/docs/laravel-dynamic-servers/v1/advanced-usage/creating-your-own-server-provider
[12] https://spatie.be/docs/laravel-server-monitor/v1/installation-and-setup