使 Laravel 使用数据库队列而不是同步

Make Laravel use database Queue instead of sync

我需要 Laravel 队列方面的帮助,如果有人可以进一步帮助我,到目前为止我已经完成了这些步骤

  1. 将 .env 文件中的 QUEUE DRIVER 更改为数据库

  2. 为队列 table 作业和 failed-jobs

    创建了迁移
    php artisan queue:table
    php artisan queue:failed-table
    

我有 运行 php artisan 迁移

  1. 创建了一个新工作

    php artisan make:job SendNewArticleNotification
    

更新:

好的,我已经创建了一个路由/queue

Route::get('/queue', function () {

    dispatch(new \App\Jobs\SendNewArticleNotification);

});

在我的工作 SendNewArticleNotification 中我有这个

<?php

namespace App\Jobs;

use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendNewArticleNotification implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        $users = User::all();

        $article = \App\Article::first();

        foreach ($users as $user) {
            $user->notify(new \App\Notifications\Published($article));
        }    


    }
}

当我点击我的 /queue 路由时,电子邮件正在发送,但他们没有使用数据库 table...发送 50 封邮件需要超过 30 秒...

更新

好吧,我终于发现 dispatch() 需要在控制器方法内部,因为控制器具有 trait dispatches jobs....现在我的作业在 DB table jobs 中排队....那么我如何在幕后触发它们?

将您的调度放入您的控制器方法中,然后 运行

php artisan queue:work