Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

I think the problem is in line 16:

'database'  => env('DB_DATABASE', $_ENV["DB_DATABASE_TWO"]),

That is Inside 'postcodes'. Should be:

'database'  => env('DB_DATABASE_TWO', $_ENV["DB_DATABASE_TWO"]),

And in your .env file:

DB_DATABASE_TWO=postcodes

Maybe that will work, because I'm not sure if $_ENV['DB_DATABASE_TWO'] is really setted correctly, so I'm assuming no.

In your model $connection should be protected, but in any case with public will work fine.

I did a local test and is working fine with 2 databases, but I used sqlite in both databases.

If you could paste maybe your models or at least part of them (the important parts) I think that will help a little bit more.

Last updated 9 years ago.
0

The env() function accepts two parameters:

  1. the key from the environment file
  2. a default value

I guess you want to use two databases. So, your database.php file should have the following entries

 'dbone' => [
            'driver'    => 'mysql',
            'host'      => env('DB_ONE_HOST', 'localhost'),
            'database'  => env('DB_ONE_DATABASE', 'defaultdb1'),
            'username'  => env('DB_ONE_USERNAME', 'defaultuser1'),
            'password'  => env('DB_ONE_PASSWORD', 'defaultpass1'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

        'dbtwo' => [
            'driver'    => 'mysql',
            'host'      => env('DB_TWO_HOST', 'localhost'),
            'database'  => env('DB_TWO_DATABASE', 'defaultdb2'),
            'username'  => env('DB_TWO_USERNAME', 'defaultuser2'),
            'password'  => env('DB_TWO_PASSWORD', 'defaultuser2'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

The .env file should have the following keys:

  • DB_ONE_HOST
  • DB_ONE_DATABASE
  • DB_ONE_USERNAME
  • DB_ONE_PASSWORD
  • DB_TWO_HOST
  • DB_TWO_DATABASE
  • DB_TWO_USERNAME
  • DB_TWO_PASSWORD

As @lazysignum said, your model shoud have a protected parameter $connection.

If you are creating a custom query in your model, you should try using

DB::connection($this->connection)->select($query)
0

@lazysignum got there first so I have marked it as the solution. But, @moinescumihai you are also correct. Thanks.

How did you find this answer? I would like to know where in the documentation it says the syntax has to be like this? Because that second parameter $_ENV["DB_DATABASE_TWO"] doesn't seem to do anything. The connection uses the first parameter of the env array.

0

I figured it out from the config files

0

Sign in to participate in this thread!

Eventy

Your banner here too?

mikelovely mikelovely Joined 26 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.