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

Asked

Viewed 1,056 times

1

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

All Urls direct to the same folder within the server -> www/meusite.

Each country has its own database: br_bancodd, us_bancodd, fr_bancodd, ... .

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

When the user accesses www.br.meusite.com, transactions shall be made in the brbancodd, So on. Every country with its bank.

Actualmete the system is in operation and to carry out this task of directing we use rewrite rules with url amigaveis to recover the URL that requested the access. Then we recover only the first 2 characters of the URL that will be br or us or fr, ... and from there we have a file config.ini which stores the access information to the database for each country, such as DB_CONNECTION(mysql),DB_HOST, DB_NAME, DB_USERNAME, DB_PASSWORD, among others, and arrow the environment to perform transactions with the relevant data bank, until the user finishes the session.

I’m trying to pass this project to the Port and I’m having difficulties in this process of recovering the first two characters and directing to the respective database.

And how, in Laravel 5, the database access credentials should be recorded in .env or config/database.php, I do not understand how to record more information from different databases in this scenario, because it only fits information from a single database, or at most 2 (one in . env and another in config/database.php).

Surely there must be some way to allow access to many databases in Laravel 5. Someone has some idea?

  • In the case of this connection: $tabUser = DB::Connection('us')->table('usertable')->get(); Return $tabUser; would be called where? in models??

1 answer

2

I found the solution. I will post here, in case of interest to the staff.

  1. Open the file .env 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=
    
  2. Open the config/database.php file and enter the credentials of each database. If you need to, you can repeat the array for as many databases as you need 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,
        ],
     ...
    
  3. Call your database with the connection name:

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

    DB::stDefaultConnection('us');

  5. Now the request waives the name of the database:

    $tabUser = DB::table('usertable')->get();
    
  • well, if downvote.... comment ! For me it worked, since nobody answered the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.