Home > OS >  How to connect multi-database in multi-tenant in Laravel tenancyforlaravel
How to connect multi-database in multi-tenant in Laravel tenancyforlaravel

Time:10-03

I am learning how to develop a laravel multi-tenant, multi-database & multi-domain web application. So I am using the tenancyforlaravel (archtechx/tenancy) package (the very first time I am using it). So my problem is how to connect the new database after creating it and insert data for it. Bellow code worked for me. But I feel there should be a way to do it using this package, And other problem is can we create relationship between two database like landload and other Dbs?

In Config/database,

'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

    'mysql2' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => null,
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

In MyController file,

    public function insertShop()
{
    $this->validate();

    $record = Tenant::updateOrCreate(['id' => $this->recordId], [
        'plan' => 'free',
        'user_id' => Auth::user()->id,
    ]);

    $record->domains()->updateOrCreate(['id' => $this->recordId], [
        'domain' =>  $this->domain
    ]);

    Config::set('database.connections.mysql2.username', $record->tenancy_db_username);
    Config::set('database.connections.mysql2.password', $record->tenancy_db_password);
    Config::set('database.connections.mysql2.database', $record->tenancy_db_name);
    DB::purge('mysql2');
    DB::reconnect('mysql2');

    ShopDetail::updateOrCreate(['id' => $this->recordId], [
        'tenant_id' =>  $record->id,
        'account_type' =>  $this->account_type,
        'business_type' =>  $this->business_type,
        'name' =>  $this->name,
    ]);

    $this->createTeam($record->id);
}

In ShopDetails model,

protected $connection = 'mysql2';

protected $fillable = [
    'tenant_id',
    'account_type',
    'business_type',
    'name',
];

CodePudding user response:

After ref the documentation in the proper way, I figure out how to connect the new DB after creating. It is called manual initialization. Only need to do this,

Replace the below code,

    Config::set('database.connections.mysql2.username', $record->tenancy_db_username);
    Config::set('database.connections.mysql2.password', $record->tenancy_db_password);
    Config::set('database.connections.mysql2.database', $record->tenancy_db_name);
    DB::purge('mysql2');
    DB::reconnect('mysql2');

To this,

tenancy()->initialize($record);

And need to remove the manual connection from the ShopDetails model,

protected $connection = 'mysql2'; //Remove this line
  • Related