如何在 运行 queue:work 命令之前调度作业

How to dispatch a job before running queue:work command

我想为电子邮件通知创建队列,此通知用于登录网站。

所以在通知文件中我实施了 ShouldQueue 然后我 运行 php artisan queue:table & php artisan migrate 在终端上成功。

之后,我将 .env 上的 QUEUE_CONNECTION 更改为数据库,并最终在 运行ing php artisan serve 之后尝试 运行 php artisan queue:table 但它完全冻结:

我什至 运行 php artisan config:clearphp artisan queue:work 再次,但仍然冻结!

更新:

正如 user1994 所建议的,我必须在 运行 执行此命令之前分派作业,但我不知道如何以及在何处分派它。

这是我的 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 [
            //
        ];
    }
}

这就是我从 LoginController 调用此通知的方式:

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

那么请你帮我解决这个问题,我将不胜感激!

提前致谢...

不是冻结,只是在等待工作。所以先 dispatch 一个 job 然后你就会看到它工作了:)

查看有关 Laravel dispatch a job

的官方文档

就像user1994&Sergio说的“就是在等工作”。在您 运行 您的 php artisan queue:work 之后,您可以继续并通过您的应用程序登录,以便发送通知。然后你就可以在控制台看到你的作业被触发了。

您也可以通过 php artisan tinker 手动触发调度,然后 运行:

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

如果您不想让控制台保持打开状态 运行ning,您可以在开发中为队列使用 sync 驱动程序。