Subdomain Multitenency Package for Laravel
Published on by Paul Redmond
Romega Digital released a package to make it easier to create subdomain-based multitenancy in your Laravel applications:
This package is meant to be a quick and easy way to add multitenancy to your Laravel application. It simply creates models and relationships for Tenants and models. The package identifies incoming traffic by subdomain, and finds a corresponding tenant in the Tenant table. If none are found or the user is not associated with a particular subdomain, the user is met with a 403 error.
Remega’s package provides multitenant support to your models through a trait called HasTenants
, and also leverages Spatie’s permissions package for some functionality.
Related: Two Best Laravel Packages to Manage Roles/Permissions
Here’s an example from the readme:
use Spatie\Permission\Traits\HasRoles;use RomegaDigital\Multitenancy\Traits\HasTenants;use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable{ use HasTenants, HasRoles; // ...}
With the HasTenants
trait in place, you can get the users associated with a Tenant model:
User::tenants()->get()
And finally here’s how you create tenants:
Tenant::createMany([ [ 'name' => 'An Identifying Name', 'domain' => 'tenant1' ], [ 'name' => 'A Second Customer', 'domain' => 'tenant2' ]]);
Along with this package, there’s an accompanying Nova package called Multitenency Nova Tool to manage the multitenancy functionality in your application.
To learn more about this Multitenancy package, you can check out the source code on GitHub at romegadigital/Multitenancy. To learn how to install and use the package, check out the Multitenency readme file.
See also: Laravel Tenancy – Multi-Tenant Package for Laravel