Laravel 5 How do I configure a queue database driver to connect to a database other than the default?

In Laravel 5.1, we can set the queue connection configurations to config/queue.php.

QUEUE_DRIVER=database

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'expire' => 60,
    ],

However, it will only use the default database connection in config/database.php.

If I have 2 databases, 1 default database mysql1in localhost and 1 database mysql2on the remote server, and the Queue table jobsis in the remote database mysql2, how do I configure the queue database driver to use the remote database mysql2? Note that the main application uses the default database in localhost.

+9
3

'connection' queue.php, ( , database.php).

'database' => [
    'connection' => 'mysql2',
    'driver' => 'database',
    'table' => 'jobs',
    'queue' => 'default',
    'expire' => 60,
], 

.

. ( ), ( ).

+25

, , , , . , Laravel .

\config\queue.php ...

'default' => env('QUEUE_DRIVER', 'database'),

.

...

    \bootstrap\cache\config.php

30 ...

 'queue' => 
array (
'default' => 'sync', 

... ...

 'queue' => 
array (
'default' => 'database',

...

php artisan config:cache

config: cache commmand config.php .

+5

You can set a variable $connectionin the model. Please note that this will only affect queries Eloquentand will not work for Builder Builder.

class Jobs extends Eloquent {
    protected $connection = "database2"     
}

This, of course, requires that you have a 2nd namedconnection in your config/database.phpfile 'database2' => [...].

+1
source

Source: https://habr.com/ru/post/1629771/


All Articles