In this article, we are going to explore how we can setup filament shield for multi-tenant laravel apps.
1. Install the package
First of all to setup filament shield for multi-tenant laravel app, make sure you have installed and configure your filament projects.
Then we will install the package. To install that, hit the following command in your terminal.
composer require bezhansalleh/filament-shield
Click here to see the official docs.
2. Publish the configuration file
After installing the package, we will publish the config file of filament shield. To publish that, hit the following command:
php artisan vendor:publish --tag="filament-shield-config"
The above command will create use the new config file.
3. Modify config file.
Just look at the configuration file, where there will be much more things for us. Mainly we need to search for tenant_model and update the value with out Tenant Model
And also, on your User Model , use the HasRoles spatie trait. This is the must.
4. Setup filament shield
After the basic command, now we are heading to setup the filament shield. For that, use the following command in your terminal:
Without multi-tenancy;
php artisan shield:setup
With Multi-Tenancy
php artisan shield:setup --tenant=App\\Models\TenantClass
The above command will:
- Publish core package config and migrations
- Run migrations too with multi-tenancy if setup
5. Setup filament shield for Admin Panel
Now, our installation is complete. The only thing that we need is to configure it to use on panel. So, for that, we just need to hit the following command:
- Without multi-tenancy
php artisan shield:install panelName
This plugin will auto register the filamentshieldplugin on our mention panel provider.
2. With multi-tenancy
If you are using multi-tenancy, then hit the following command:
php artisan shield:install admin --tenant --generate-relationships
This command will register the plugin as well as the middleware for checking the permission.
->plugins([
FilamentShieldPlugin::make(),
])
->tenantMiddleware([
SyncShieldTenant::class,
], isPersistent: true)
Some IMPORTANT COMMANDS
1. Generate policies for all models
php artisan shield:generate --all or any of following:
--all Generate for all entities
--option=[OPTION] Override generator option
--resource=[RESOURCE] Specific resources
--page=[PAGE] Specific pages
--widget=[WIDGET] Specific widgets
--exclude Exclude entities
--ignore-config-exclude Ignore config excludes
--panel[=PANEL] Panel ID to get the components(resources, pages, widgets)
2. Automatically create role/permission seeder
php artisan shield:seeder
If you created this file already, you can remove and then hit the command again.
3. Create a Super Admin
php artisan shield:super-admin [--user=] [--panel=] [--tenant=]
Conclusion
In this way, you can easily setup filament shield in your multi-tenant applications.
I followed exactly your video tutorial and when i run this command:
php artisan shield:setup –tenant=App\\Models\Team
this will delete all the contents inside from the files filament-shield.php and permissions.php
and when i run this command:
php artisan shield:install admin –tenant –generate-relationships
i got error:
In Container.php line 1019:
Target class [cache] does not exist.
In Container.php line 1017:
Class “cache” does not exist
Do you any idea? what’s wrong?
Can you try to clear cache and everything,
php artisan optimize:clear
and try it again.
php artisan shield:setup –tenant = App\\Models\\Team (2 backslash)
Do you know the solution? I faces the same problem
Do you know the solution. I face the same issue