Is it possible to access multiple databases in the same Laravel 5 project?

1

The scenario is as follows: I have a web site that can be accessed by 5 different countries - > br.meusite.com , us.meusite.com , fr.meusite.com , ....

All URLs point to the same folder inside the server - > www/meusite .

Each country has its data base: br_bancodd , us_bancodd , fr_bancodd , ....

In short, each country has its database, but all countries share the same files as the root folder www/meusite .

When the user accesses www.br.meusite.com , the transactions will be done in the brbancodd database, and so on. each country with its bank.

Currently the system is up and running to do this targeting task we use rewrite rules with url amigaveis to retrieve the URL that requested the access. Then we retrieve only the first 2 characters of the URL which in this case will be br or us or fr , ... and from there we have a config.ini file that stores the database access information for each country, such as DB_CONNECTION(mysql),DB_HOST, DB_NAME, DB_USERNAME, DB_PASSWORD , among others, and set the environment to perform transactions with the relevant database, until the user logs out.

I am trying to pass this project to laravel and I am having difficulties in this process of recovering the first two characters and heading to the respective database.

And because, in laravel 5, the database access credentials should be written to .env or config/database.php , I do not understand how to write more information from the different databases in this scenario, a single database, or at most 2 (one in .env and one in config / database.php).

Surely there must be some way to allow access to many databases in laravel 5. Does anyone have idea idea?

    
asked by anonymous 25.07.2016 / 01:38

1 answer

2

I found the solution. I'll post here, if interested to the staff.

  • Open the .env file and delete the lines below:

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=br_bancodd
    DB_USERNAME=root
    DB_PASSWORD=
    
  • Open the config / database.php file and put the credentials for each database. If you need to, you can repeat the array for as many databases as needed in your application. Ex:

    'br' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'br_bancodd'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
    'us' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'us_bancodd'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
     ...
    
  • Call your database with the connection name:

    $tabUser = DB::connection('us')->table('usertable')->get();
    return $tabUser;
    
  • It's ready. You just need to set the deafult connection to avoid writing the database name all the time from the session .

    DB::stDefaultConnection('us');

  • Now the request dispenses the name of the database:

    $tabUser = DB::table('usertable')->get();
    
  • 25.07.2016 / 04:59