I am working on an application that is a multi multi-tenant multi DB architecture, which basically means that each tenant has their own database, and not all tenants living in the same database.
Now I am constantly struggling with the fact that I cannot successfully change database connections, and I'm not sure if this is the right way to do this.
My database configuration file looks like this:
'connections' => [
'archive' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'tenant' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
, , (), , (), . , , . , username
, , , , . , username
, , , . , , .
:
$this->changeDb($request->input('username'));
$this->saveUsernameToSession($request);
if (\Auth::attempt(['email' => $request->input('email'),
'password' => $request->input('password')])) {
return redirect('/');
}else{
$request->session()->pull('username');
}
changeDB()
- , , , .
\Config::set('database.connections.tenant.database', $username);
\Config::set('database.default', 'tenant');
, , . , , . .
? , . , Laravel , / .
, , , , , , .
, Laravel 5?
, , - . , , ?
, , :
public function showUsersForTenant($id)
{
$tenant = Tenant::findOrFail($id);
\Config::set('database.connections.tenants.database', $tenant->username);
$users = User::on('tenant')->get();
return response()->json($users, 200);
}
() , . .
, multi multi tenancy multi database, -, , / .
https://github.com/orchestral/tenanti
https://github.com/laraflock/multi-tenant
https://github.com/uxweb/laravel-multi-db
, // Laravel 5?