Fokwa Best
Fokwa Best

Reputation: 3515

How can I use multiple Laravel job tables?

I have two Laravel apps sharing a database and jobs table. The crontab entries below are responsible for running the queue:work commands:

# App2 - path to app 2 - App with Account model and accounts table
* * * * * uname cd /app2/ && php artisan schedule:run

# App1 - path to app 1 - App with User Model and users table
* * * * * uname cd /app1/ && php artisan schedule:run

Here is my issue, when a user in app1 tries to recover his password by submitting his email, the accounts table is rather queried instead of the users table giving me a wrong name in the email that is sent to the user. What can be the problem?

My queue driver is database, this is the email code:

public function emailResetLink(CanResetPasswordContract $user, 
                               string $token, Closure $callback = null)
{
    $view = $this->emailView;

    return $this->mailer->queue(
        $view,
        compact('token', 'user'),
        function ($m) use ($user, $token, $callback) {
            $m->to($user->getEmailForPasswordReset());

            if (! is_null($callback)) {
                call_user_func($callback, $m, $user, $token);
            }
        }
    );
}

This is in the email view:

Dear {{ $user->name }},<br/><br/> <!-- I get the wrong name --> 
{{ trans('passwords.password_email_text_one') }}

Upvotes: 2

Views: 2450

Answers (1)

lifekent
lifekent

Reputation: 300

Make a few connections inside config/queue.php, setting a different table name on each:

<?php
return [
    'connections' => [
        'accountConnection' => [
            'driver' => 'database',
            'table' => 'accounts',
            'queue' => 'accountqueue',
            'expire' => 60,
        ],
        'userConnection' => [
            'driver' => 'database',
            'table' => 'users',
            'queue' => 'userqueue',
            'expire' => 60,
        ],
    ],
];

Then specify a separate connection when adding a job,

Queue::connection("accountConnection")->push($job);
// or
$job->dispatch()->onConnection("accountConnection");

And after, use

php artisan queue:work accountConnection

To run the specific queue connection.

To create a migration for the queue table, run

php artisan queue:table

Once migration is created you can copy it, rename the table, and make sure table names are set correctly in config/queue.php.

Upvotes: 5

Related Questions