php artisan队列:工作或听不工作Laravel 5.4

时间:2018-05-10 11:41:33

标签: php laravel-5.4

我已经安装了laravel 5.4并希望确认用户注册,通过带有队列的电子邮件,成功创建了作业表,数据也插入到表用户中,但是当我运行 PHP artisan队列时它没有输出:work 命令。 我的Gmail邮箱里什么也没收到 我花了很多时间来解决它,但仍然无法正常工作。非常感谢任何帮助。非常感谢。

Jobs / SendVerificationEmail类:

    class SendVerificationEmail implements ShouldQueue
{
  protected $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function handle()
    {
        $data = ['email_token' => $this->user->email_token];

        Mail::send('email.email', $data, function($message) {
            $message->subject('This mail send by Queue Laravel 5.4');
            $message->to($this->user->email);
        });
    }
}

Contoller / RegistrationController类:

 class RegistrationController extends Controller
{
    public function create()
    {
        return view('register');
    }

    public function register(Request $request)
    {
        //create user
        $user = new User;
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->email_token = base64_encode($request->email);

        $user->save();
        dispatch(new SendVerificationEmail($user));

        return view('verification');

        //add role by default the new registred saved as a User Role
        $user->roles()->attach(Role::where('name', 'User')->first());

        // redirect
        //return redirect('/posts');

        //login
        auth()->login($user);

    }
    public function verify($token)
    {
        $user = User::where('email_token',$token)->first();
        $user->verified = 1;
        if($user->save())
        {
            return view('emailconfirm',['user'=>$user]);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在SendVerificationEmail作业的构造函数中设置连接以及您不想使用的队列,例如

   public function __construct($user)
   {
        $this->queue = 'default'; //choose a queue name
        $this->connection = 'database';
        $this->user = $user;
   }

然后开始处理这些作业使用artisan命令

 artisan queue:work database --queue=default

此外,在您的SendVerificationEmail作业中,在文件顶部添加行public $tries = 3;,现在在此次尝试次数后,作业将进入失败的作业队列,您可以检查放置它的异常内容那里。