Tenanti allow you to manage multi-tenant data schema and migration manager for your Laravel application.
Laravel | Tenanti |
---|---|
4.2.x | 2.2.x |
5.0.x | 3.0.x |
5.1.x | 3.1.x@dev |
To install through composer, simply put the following in your composer.json
file:
{
"require": {
"orchestra/tenanti": "3.1.*"
}
}
And then run composer install
to fetch the package.
You could also simplify the above code by using the following command:
composer require "orchestra/tenanti=3.1.*"
Next add the following service provider in app/config/app.php
.
'providers' => [
// ...
'Orchestra\Tenanti\TenantiServiceProvider',
'Orchestra\Tenanti\CommandServiceProvider',
],
The command utility is enabled via Orchestra\Tenanti\CommandServiceProvider.
Update your App\Providers\ConfigServiceProvider
to include following options:
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ConfigServiceProvider extends ServiceProvider
{
public function register()
{
config([
'orchestra/tenanti::drivers.user' => [
'model' => 'App\User',
'path' => base_path('database/tenanti/user'),
],
]);
}
}
You can customize, or add new driver in the configuration. It is important to note that model
configuration only work with Eloquent
instance.
For each driver, you should also consider adding the migration path into autoload. To do this you can edit your composer.json
.
{
"autoload": {
"classmap": [
"database/tenant/users"
]
}
}
Now that we have setup the configuration, let add an observer to our User
class (preferly in App\Providers\AppServiceProvider
):
<?php namespace App\Providers;
use App\User;
use App\Observers\UserObserver;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
User::observe(new UserObserver);
}
}
and your App\Observers\UserObserver
class should consist of the following:
<?php namespace App\Observers;
use Orchestra\Tenanti\Observer;
class UserObserver extends Observer
{
public function getDriverName()
{
return 'user';
}
}
Tenanti include additional command to help you run bulk migration when a new schema is created, the available command resemble the usage available from php artisan migrate
namespace.
Command | Description |
---|---|
php artisan tenanti:install {driver} | Setup migration table on each entry for a given driver. |
php artisan tenanti:make {driver} {name} | Make a new Schema generator for a given driver. |
php artisan tenanti:migrate {driver} | Run migration on each entry for a given driver. |
php artisan tenanti:rollback {driver} | Rollback migration on each entry for a given driver. |
php artisan tenanti:reset {driver} | Reset migration on each entry for a given driver. |
php artisan tenanti:refresh {driver} | Refresh migration (reset and migrate) on each entry for a given driver. |
Instead of using Tenanti with a single database connection, you could also setup a database connection for each tenant.
By introducing a migration
config, you can now setup the migration table name to be tenant_migrations
instead of user_{id}_migrations
.
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ConfigServiceProvider extends ServiceProvider
{
public function register()
{
config([
'orchestra/tenanti::drivers.user' => [
'model' => 'App\User',
'migration' => 'tenant_migrations',
'path' => base_path('database/tenanti/user'),
],
]);
}
}
Adding an override method for getConnectionName()
would allow you to force the migration to be executed on the desire connection.
<?php namespace App\Observers;
use Orchestra\Tenanti\Observer;
class UserObserver extends Observer
{
public function getDriverName()
{
return 'user';
}
public function getConnectionName()
{
return 'tenant_{id}';
}
}