How to build a custom connection outside of database.php in Laravel 5.4 -
i building system uses multiple databases. have custom config file using has controls in it. file not put through version control.
i these databases independant of git. looking build custom connection without using config/database.php
i of course remove config/database.php git want keep neat , make use of custom config file.
here clientconfig.php found in /config folder
<?php $clientdb = ''; if(isset($_server['server_name'])) { $apidomain = $_server['server_name']; if ( $apidomain == 'www.example.com' ) { $clientdb = 'clientdb_1'; } } return [ 'client_db' => $clientdb ];
i add connections in file found in 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', 'example'), 'username' => env('db_username', 'example'), 'password' => env('db_password', 'example'), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], 'clientdb_1' => [ 'driver' => 'mysql', 'host' => env('db_host', 'localhost'), 'port' => env('db_port', '3306'), 'database' => ('clientdb_1'), 'username' => env('db_username', 'example'), 'password' => env('db_password', 'example'), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ],
is there clean way can done?
edit: have consired .env file messy considering may have different amount of databases etc. still adding them database.php trying avoid.
i able add own new connection in file using config()
.
if ( $apidomain == 'www.example.com' ) { $appurl = $apidomain; $clientdb = 'clientdb_1'; config(['database.connections.clientdb_1' => array( 'driver' => 'mysql', 'host' => env('db_host', 'localhost'), 'port' => env('db_port', '3306'), 'database' => $clientdb, 'username' => 'example', 'password' => 'example', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, )]); }
nb: did not work unless custom config file loaded after database.php. because in alphabetical order had rename file z_clientconfig.php
Comments
Post a Comment