put it in a middleware, and use that middleware for those controllers so when those controllers are called it just automatically sets the config.
Jul 31, 2019
2
Level 1
Persist user database connection
Hello,
I have in a database the user table and the user_database table, when the user logs in I get the connection information from his database and so I work with the 2 databases.
'system' => [
'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' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'tenant' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => '',
'port' => env('DB_PORT', '33060'),
'database' => '',
'username' => '',
'password' => '',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
However, when I need the user database I connect to the controller with Config :: set
Config::set('database.connections.tenant.host', \Auth::user()->database->db_host);
Config::set('database.connections.tenant.database', \Auth::user()->database->db_name);
Config::set('database.connections.tenant.username', \Auth::user()->database->db_user);
Config::set('database.connections.tenant.password', \Auth::user()->database->db_pass);
But always doing this on the controller when I need to connect to the bank is not correct.
How can I persist this connection throughout my application?
thanks
Please or to participate in this conversation.