A full-featured multi-tenant app with Laravel Part 2 — Roles and Permissions
Part 0, Part 1, Part 2 👇, Part3, Part4, Part 5, Part 6, Part 7
In this part of writing a complete Laravel multi-tenant app series, we’ll accomplish the following tasks:
✅ Install and configure a package for managing roles and permissions
✅ Seed the database with some roles and permissions
✅ Give all the power needed for an admin user to administer the new tenant
1. Install and configure a package for managing roles and permissions
We’ll use spatie’s excellent laravel-permission package for managing roles and permissions in our multi-tenant app (MTA). Go and read the documentation first. You don’t need to understand everything right now but just be familiar with some of the concepts and commands.
Once you are done, add the package:
composer require spatie/laravel-permissionNow, we have to make this package aware of multi-tenancy setup by setting proper connection name to the package’s core Eloquent models —Permission and Role.
We obviously don’t want to change these models in the vendor directory to make them multi-tenancy aware. We have to create our own Role and Permission models and let the package know to use these models instead of packaged models. This can be done by configuring the package’s vendor config file. Let’s publish it first:
php artisan vendor:publishThen select:
Provider: Spatie\Permission\PermissionServiceProvider
This will publish two files — config/permission.php and xxxx_xx_xx_xxxxxx_create_permission_tables.php.
The migration file xxxx_xx_xx_xxxxxx_create_permission_tables.php needs to be moved to database/migrations/tenant folder. Go ahead and move it.
Let’s make the changes we need to config/permission.php:
'models' => [
'permission' => App\Permission::class,
'role' => App\Role::class,
],As you can see, we told the package to use App\Permission::class and App\Role::class Eloquent models for permission and role respectively. These models don’t exist yet so let’s make them:
php artisan make:model Permission
php artisan make:model RoleThese two new models are bare-bones Eloquent models, which, as they are, not going to do any good for us. We have to make them aware of both laravel-permission package and our multi-tenancy setup.
For the first, we’ll use the magic of inheritance and for the latter, we’ll use a trait.
Let’s edit Permission.php model first:





