0

I have multiple database configs in my config\app.php. Like follow example:

'connections' => [
    'database1' => [
        /**
         * ...
         */
        'database' => database1_env('database'),
        'username' => database1_env('username'),
        'password' => database1_env('password'),
        /**
         * ...
         */
    ],
    'database2' => [
        /**
         * ...
         */
        'database' => database2_env('database'),
        'username' => database2_env('username'),
        'password' => database2_env('password'),
        /**
         * ...
         */
    ]
]

I have configured and almost everything works fine, the problem is async. Because I have the same Notification [ Mail to request password ] to both databases, and I don't know how to force the selection to the correct database.

Thiago Valente
  • 673
  • 8
  • 25
  • Does this answer your question? [How to use multiple databases in Laravel](https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel) – Abdulla Nilam Sep 12 '22 at 11:45

1 Answers1

0

The fastest way without messing up with a $_SERVER['argv'] variable (which contains CLI input arguments) would be to create separate .env file, like .env.db1 and .env.db2 after which you could run your queue using --env=db1 or --env=db2 parameter since queue:work is artisan command. This may look like not best solution but trust me that you will avoid many issues with underlying parts of the framework (mostly cached configs).

See: https://laravel.com/docs/6.x/configuration#environment-configuration

You misspelled a little your question because I think that you have a job that takes one of these databases to work with, not two of them at once.

However, if your job is running on both databases at the same time, then instead of creating a standalone .env just switch a connection using the model's static on method.

See github source code: https://github.com/laravel/framework/blob/23ceaf9fa2b442362d566c94dfdc063275f0ba11/src/Illuminate/Database/Eloquent/Model.php#L431

Bartłomiej Sobieszek
  • 2,692
  • 2
  • 25
  • 40
  • I have the multiple ```.env``` to single database connection and I can running in same time both databases queue. How can I storage the correct database to call in queue? – Thiago Valente Nov 26 '19 at 01:04