How to dispatch a job before running queue:work command

蹲街弑〆低调 提交于 2021-02-10 05:32:43

问题


I want to make queue for email notifications and this notification is used for logging into website.

So at the notification file I implemented ShouldQueue and then I ran php artisan queue:table & php artisan migrate on terminal successfully.

After that I changed QUEUE_CONNECTION on .env to database and finally tried to run php artisan queue:table after runing php artisan serve but it freezes at all:

I even ran php artisan config:clear and php artisan queue:work again but still freezes!

UPDATE:

As user1994 suggests, I have to dispatch a job before running this command, but I don't know how and where to dispatch it.

Here is my LoginToWebsiteNotification class:

class LoginToWebsiteNotification extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('u Loggedin')
            ->view('emails.login-to-website');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

And this is how I called this notification from LoginController:

protected function authenticated(Request $request, $user)
{
    $user->notify(new LoginToWebsiteNotification());
    return $this->loggendin($request , $user);
}

So would you please help me out with this, I would really appreciate that!

Thanks in advance...


回答1:


Its not frozen, its just waiting for a job. So firstly dispatch a job and then you will see it working :)

Have a look at this official documentation about Laravel dispatch a job




回答2:


Like user1994 & Sergio said, "it's just waiting for a job". After you run your php artisan queue:work, you can go ahead and login through your application so that the notification can be dispatched. You can see your job being triggered in the console then.

You could also manually trigger a dispatch through php artisan tinker, then run:

User::find(1)->notify(new App\Notifications\LoginToWebsiteNotification)

If you don't like to keep your console open and running, you could use sync driver for your queue in development.



来源:https://stackoverflow.com/questions/65300381/how-to-dispatch-a-job-before-running-queuework-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!