IgorAlves
IgorAlves

Reputation: 5550

Laravel 5 - How can I set more than 1 database credentials for the same laravel project (if it is possible)?

I have a project that must to be accessed through these 2 URLs us_myproject.com and es_myproject.com.

They share the same folders and code. In fact there are only one laravel project: htdocs/myproject.

depending on URL the information must to come from us_db or es_db, based on the URL used.

So my question is how to set .env db credentials to point to these 2 different databases. I have 2 different databases, 2 different user and 2 different password. How can I accomplish this task?

I have this follow code that works:

URL: us_myproject.com/dbtest


.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=us_db
DB_USERNAME=root
DB_PASSWORD=''

Route::get('dbtest',function(){//Works good
 $tblusertypes=DB::table("tblusertypes")->get();
 return $tblusertypes;
});

If I do URL: es_myproject.com/dbtest should query against es_db but I dont know how to set this. Any Idea?

Upvotes: 1

Views: 104

Answers (2)

Naveed
Naveed

Reputation: 929

Reading your comments on an other answer, I have come up with the following solution:

  1. Open config/database.php:

    <?php

    return [
        ...

        'connections' => [

            ...

            'mysql' => [
                'driver'    => 'mysql',
                'host'      => env('DB_HOST', 'localhost'),
                'database'  => env('DB_DATABASE', 'forge'),
                'username'  => env('DB_USERNAME', 'forge'),
                'password'  => env('DB_PASSWORD', ''),
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
                'strict'    => false,
            ],

            ...

        ],

        ...

    ];
  1. Edit the file like this:

    <?php

    $config = [
        ...

        'connections' => [

            ...

            'mysql' => [
                'driver'    => 'mysql',
                'host'      => env('DB_HOST', 'localhost'),
                'database'  => env('DB_DATABASE', 'forge'),
                'username'  => env('DB_USERNAME', 'forge'),
                'password'  => env('DB_PASSWORD', ''),
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
                'strict'    => false,
            ],

            ...

        ],

        ...

    ];

    if( Request::server('HTTP_HOST') == 'es_myproject.com' )
    {
        $config['connections']['mysql'] = [
            'driver' => 'mysql',
            'host' => 'esHOST',
            'database' => 'esDB',
            'username' => 'esUSER',
            'password' => 'esPASSWORD',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
        ];
    }

    return $config;

Here I assume that the .env file contains the database credentials for us_myproject.com.

Upvotes: 0

Ahmed Sayed Sk
Ahmed Sayed Sk

Reputation: 684

Yes you can set more than 1 database in Laravel, open config/database in connection array set a new database like same thing in mysql array

In .env file you can detect what database is default

To call it: $result = DB::connection('my_new_database')->table('my_table')->get();

Upvotes: 1

Related Questions