May have to put some logic in a if else type construct, or have a db table holding the conn info. Double check https://laravel.com/docs/5.3/database#using-multiple-database-connections
Laravel Dynamic Database Connections
Recently I have been trying to use a dynamic database connection with Laravel, but I have been running into issues.
I have been doing research for the past week, but the answer to this problem seems to be non-existent.
So, what I want to do is have a database connection that is SET and SAVED* after a user logs in. A user is tied to a specific company and each company will have a separate database.
This is my database.php
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'club' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => '',
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
And this is my authenticated method override to Laravel's default one
protected function authenticated(Request $request, $user)
{
Config::set('database.connections.club.database', $user->club->db_name);
DB::purge('club');
DB::reconnect('club');
}
However, after setting the config in the authenticated when I try to access the brand new connection, it reverts to the old database.php connection.
Is there a way to get this connection to STICK (SAVE) for the rest of a user's session. IE store the new connection someone. (I know I can't use the session to store the new database connection)
Please help, Nick
EDIT*
If I put this inside the database.php
return [ // There is more code here that is irreverent to the problem.
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'club' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => Auth::user()->club->db_name,
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
];
I get the error Auth not found and a bunch of defined errors.
If I use config(['connection.database.club.database', Auth::user()->club->db_name]); it only stores it for 1 SINGLE request and doesn't carry over to other requests.
Please or to participate in this conversation.