1

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?

IgorAlves
  • 5,086
  • 10
  • 52
  • 83

2 Answers2

1

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();

Ahmed Sayed Sk
  • 584
  • 6
  • 20
  • NIce, and how can I make a connection between the URL and the database? I mean, if the URL will be `es_myproject.com` how can I set `DB::connection('es_myproject')` or `us_myproject.com` set to `DB::connection('us_myproject')`? – IgorAlves Jul 24 '16 at 21:07
  • I don't understand your question – Ahmed Sayed Sk Jul 24 '16 at 21:09
  • The question is where should I create the relationship between the url and the database. I mean if the user type `es_myproject.com` the database by default should be `es_db`. If the user type `us_myproject.com` the database by default should be `us_db`. So, there should be a if conditions some where, correct? – IgorAlves Jul 24 '16 at 21:16
0

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.

Naveed
  • 879
  • 1
  • 9
  • 20
  • Hi, I see what you did. So, your idea is to change the array content - the credentials, based on the HTTP_HOST. That is a good option. But it raises a question. That change will be available until the end of the SESSION, I mean, lets say the user get access to the home page, the Session will be set to a specific environment, in this case, Spain ( es ), and later, he goes to the other page and try to insert some sturff. The environment is already sit? – IgorAlves Jul 25 '16 at 14:57
  • 2
    I don't think it has anything to do with SESSION. It's just a matter of which database you use for a specific host. When they switch to another host anytime, the credentials will be switched. – Naveed Jul 26 '16 at 04:22