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.
The env() function accepts two parameters:
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:
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)
@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.
TRY THIS ONE>>>>>
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community