orchestra / tenanti
Tenant based Database Schema Manager for Laravel
Fund package maintenance!
Liberapay
paypal.me/crynobone
Installs: 115 409
Dependents: 0
Suggesters: 0
Security: 0
Stars: 587
Watchers: 27
Forks: 53
Open Issues: 4
Requires
- php: ^7.3 || ^8.0
- illuminate/console: ^8.40
- illuminate/filesystem: ^8.40
- illuminate/support: ^8.40
- orchestra/model: ^6.0
- orchestra/support: ^6.1
Requires (Dev)
- laravel/tinker: ^2.5
- nunomaduro/larastan: ^0.7.12
- orchestra/canvas: ^6.1
- orchestra/testbench: ^6.17
- dev-master / 7.0.x-dev
- 6.x-dev
- v6.3.0
- v6.2.0
- v6.1.0
- v6.0.0
- 5.x-dev
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- 4.x-dev
- v4.1.0
- v4.0.0
- 3.8.x-dev
- v3.8.3
- v3.8.2
- v3.8.1
- v3.8.0
- 3.7.x-dev
- v3.7.1
- v3.7.0
- 3.6.x-dev
- v3.6.6
- v3.6.5
- v3.6.4
- v3.6.3
- v3.6.2
- v3.6.1
- v3.6.0
- 3.5.x-dev
- v3.5.2
- v3.5.1
- v3.5.0
- v3.5.0-BETA1
- 3.4.x-dev
- v3.4.2
- v3.4.1
- v3.4.0
- v3.4.0-BETA3
- v3.4.0-BETA2
- v3.4.0-BETA1
- 3.3.x-dev
- v3.3.5
- v3.3.4
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- v3.3.0-BETA1
- 3.2.x-dev
- v3.2.9
- v3.2.8
- v3.2.7
- v3.2.6
- v3.2.5
- v3.2.4
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- v3.2.0-BETA1
- 3.1.x-dev
- v3.1.10
- v3.1.9
- v3.1.8
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- 3.0.x-dev
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
This package is auto-updated.
Last update: 2024-11-22 06:33:22 UTC
README
Tenanti allow you to manage multi-tenant data schema and migration manager for your Laravel application.
Version Compatibility
Installation
To install through composer, run the following command from terminal:
composer require "orchestra/tenanti"
Configuration
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
.
Aliases
To make development easier, you could add Orchestra\Support\Facades\Tenanti
alias for easier reference:
'aliases' => [ 'Tenanti' => Orchestra\Support\Facades\Tenanti::class, ],
Publish Configuration
To make it easier to configuration your tenant setup, publish the configuration:
php artisan vendor:publish
Usage
Configuration Tenant Driver for Single Database
Open config/orchestra/tenanti.php
and customize the drivers.
<?php return [ '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.
Setup migration autoload
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
.
composer.json
{ "autoload": { "classmap": [ "database/tenant/users" ] } }
Setup Tenantor Model
Now that we have setup the configuration, let add an observer to our User
class:
<?php namespace App; use App\Observers\UserObserver; use Orchestra\Tenanti\Tenantor; use Illuminate\Notifications\Notifiable; use Orchestra\Tenanti\Contracts\TenantProvider; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements TenantProvider { use Notifiable; /** * Convert to tenantor. * * @return \Orchestra\Tenanti\Tenantor */ public function asTenantor(): Tenantor { return Tenantor::fromEloquent('user', $this); } /** * Make a tenantor. * * @return \Orchestra\Tenanti\Tenantor */ public static function makeTenantor($key, $connection = null): Tenantor { return Tenantor::make( 'user', $key, $connection ?: (new static())->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:
<?php namespace App\Observers; use Orchestra\Tenanti\Observer; class UserObserver extends Observer { public function getDriverName() { return 'user'; } }
Console Support
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.
Multi Database Connection Setup
Instead of using Tenanti with a single database connection, you could also setup a database connection for each tenant.
Configuration Tenant Driver for Multiple Database
Open config/orchestra/tenanti.php
and customize the drivers.
<?php return [ 'drivers' => [ '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
.
Database Connection Resolver
For tenanti to automatically resolve your multiple database connection, we need to setup the resolver. You can do this via:
<?php namespace App\Providers; use Orchestra\Support\Facades\Tenanti; class AppServiceProvider extends ServiceProvider { public function boot() { Tenanti::connection('tenants', function (User $entity, array $config) { $config['database'] = "acme_{$entity->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.
Setting Default Database Connection
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\Request
object.