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@dev |
To install through composer, simply put the following in your composer.json
file:
{
"require": {
"orchestra/tenanti": "3.0.*"
}
}
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.0.*"
Next add the following service provider in app/config/app.php
.
'providers' => array(
// ...
'Orchestra\Tenanti\TenantiServiceProvider',
'Orchestra\Tenanti\CommandServiceProvider',
),
The command utility is enabled via Orchestra\Tenanti\CommandServiceProvider.
First, let's export the configuration to your application configuration folder to customize the option:
php artisan config:publish orchestra/tenanti
Now when you browse to app/config/packages/orchestra/tenanti/config.php
you should be welcome with the following config:
<?php
return array(
// ...
'drivers' => array(
'user' => array(
'model' => 'User',
'path' => app_path('database/tenant/users'),
),
),
);
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": [
"app/database/tenant/users"
]
}
}
Now that we have setup the configuration, let add an observer to our User
class (preferly in app/start/global.php
):
<?php
User::observe(new UserObserver);
and your UserObserver
class should consist of the following:
<?php
class UserObserver extends \Orchestra\Tenanti\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
return array(
// ...
'drivers' => array(
'user' => array(
'model' => 'User',
'migration' => 'tenant_migrations',
'path' => app_path('database/tenant/users'),
),
),
);
Adding an override method for getConnectionName()
would allow you to force the migration to be executed on the desire connection.
<?php
class UserObserver extends \Orchestra\Tenanti\Observer
{
public function getDriverName()
{
return 'user';
}
public function getConnectionName()
{
return 'tenant_{id}';
}
}