[Package] Multi-tenant Database Schema Manager for Laravel
[Package] Multi-tenant Database Schema Manager for Laravel
Tenanti allow you to manage multi-tenant data schema and migration manager for your Laravel application.
| Laravel | Tenanti |
|---|---|
| 5.5.x | 3.5.x |
| 5.6.x | 3.6.x |
| 5.7.x | 3.7.x |
| 5.8.x | 3.8.x |
| 6.x | 4.x |
| 7.x | 5.x |
| 8.x | 6.x |
To install through composer, run the following command from terminal:
composer require "orchestra/tenanti"Next add the following service provider in config/app.php.
'providers' => [
// ...
Orchestra\Tenanti\TenantiServiceProvider::class,
Orchestra\Tenanti\CommandServiceProvider::class,
],The command utility is enabled via
Orchestra\Tenanti\CommandServiceProvider.
To make development easier, you could add Orchestra\Support\Facades\Tenanti alias for easier reference:
'aliases' => [
'Tenanti' => Orchestra\Support\Facades\Tenanti::class,
],To make it easier to configuration your tenant setup, publish the configuration:
php artisan vendor:publishOpen config/orchestra/tenanti.php and customize the drivers.
[
'user' => [
'model' => App\User::class,
'paths' => [
database_path('tenanti/user'),
],
'shared' => true,
],
],
];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 (if it not already defined). 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:
getConnectionName()
);
}
/**
* The "booting" method of the model.
*/
protected static function boot()
{
parent::boot();
static::observe(new UserObserver);
}
}and your App\Observers\UserObserver class should consist of the following:
[
'user' => [
'model' => App\User::class,
'paths' => [
database_path('tenanti/user'),
],
'shared' => false,
],
],
];By introducing a migration config, you can now setup the migration table name to be tenant_migrations instead of user_{id}_migrations.
For tenanti to automatically resolve your multiple database connection, we need to setup the resolver. You can do this via:
getKey()}";
// refer to config under `database.connections.tenants.*`.
return $config;
});
}
}Behind the scene, $config will contain the template database configuration fetch from "database.connections.tenants" (based on the first parameter tenants). We can dynamically modify the connection configuration and return the updated configuration for the tenant.
Alternatively you can also use Tenanti to set the default database connection for your application:
use App\User;
use Orchestra\Support\Facades\Tenanti;
// ...
$user = User::find(5);
Tenanti::driver('user')->asDefaultConnection($user, 'tenants_{id}');Most of the time, this would be use in a Middleware Class when you resolve the tenant ID based on
Illuminate\Http\Requestobject.
No open issues yet, or sync has not completed.