In Laravel 5.1, we can set the Queue connection configurations in 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 database, 1 default database mysql1
in localhost, and 1 database mysql2
in a remote server, and the Queue jobs
table is in the remote database mysql2
, how can I configure the Queue database driver to use the remote mysql2
database? Please note that the main App is using the default database in localhost.
In . env file you can set DB_CONNECTION with your database name and applicable databases are given in /config/database. php which are (SQLite, MySQL, pgSQL, SQLSRV) after that just type your username, password, and database name and you can use that database with port number.
To change its default database type, edit the file config/database. php: Search for 'default' =>env('DB_CONNECTION', 'mysql') Change it to whatever required like 'default' =>env('DB_CONNECTION', 'sqlite')
Laravel Queues Queue Driver Configuration A queue driver is the handler for managing how to run a queued job, identifying whether the jobs succeeded or failed, and trying the job again if configured to do so.
You can use the 'connection'
parameter in queue.php
to set the correct database connection ( from the ones you've defined in database.php
).
'database' => [
'connection' => 'mysql2',
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
],
I was looking for the same thing and found it in the source code.
NOTE: This will not only read the jobs from this connection ( when running the queue ), but also write them to this connection ( when dispatching a new Job ) .
The best answer here did not work for me, not to say it isn't the best answer for a different issue than mine. My issue was that Laravel did not cache my config settings.
After going into file \config\queue.php and changing the default driver...
'default' => env('QUEUE_DRIVER', 'database'),
The queue was still running on the sync driver.
I then checked the file...
\bootstrap\cache\config.php
Around line 30 I saw this...
'queue' =>
array (
'default' => 'sync',
...but to connect to the database, it should be...
'queue' =>
array (
'default' => 'database',
This resolved the issue...
php artisan config:cache
Running the config:cache commmand rewrites the config.php file to the current driver settings.
You can set the $connection
variable in the model. Note that this will only affect Eloquent
queries and will not work for the Fluid Query Builder.
class Jobs extends Eloquent {
protected $connection = "database2"
}
This would of course require you to have a 2nd named
connection in your config/database.php
file that is 'database2' => [...]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With