1

I am building a system that uses multiple databases. I currently have a custom config file which I am using that has some controls in it. This file is not put through version control.

I would like these databases to be independant of git. I am looking to build a custom connection without using config/database.php

I could of course remove config/database.php from git but I want to keep it neat and make use of my custom config file.

Here is my clientconfig.php to be 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 would like to add my connections in that file too that are 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 a clean way this can be done?

EDIT: I have consired the .env file but it is too messy considering I may have different amount of databases etc. I will still be adding them to database.php which is what I am trying to avoid.

3

1 Answer 1

2

I was able to add my own new connection in that file by 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: This did not work unless my custom config file was loaded after database.php. Because it was in alphabetical order I had to rename my file to z_clientconfig.php

Sign up to request clarification or add additional context in comments.

1 Comment

Great! this can be over-right default connection "mysql" also thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.