简体   繁体   中英

php artisan queue:work or listen not working Laravel 5.4

I have installed laravel 5.4 and want to confirm user registration, by email with queue, jobs table is created successfully, data are inserted as well in table user, but it outputs nothing when I run PHP artisan queue: work command. and I received nothing in my gmail box I've spent many hours to resolve it, but still not working. Any help would be really appreciated, thanks much.

Jobs/SendVerificationEmail Class:

    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:

 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]);
        }
    }
}

In the constructor of your SendVerificationEmail job set the connection and which queue you wan't to use, for example,

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

Then to start processing these jobs use the artisan command

 artisan queue:work database --queue=default

Also, in your SendVerificationEmail job add the line public $tries = 3; at the top of the file, now after this number of tries the job goes on the failed jobs queue and you can check the contents of the exception which put it there.

好吧,如果您在 que:listen 之后什么也没看到,并且邮件是通过普通邮件门面发送的,您应该在 .env que_connection=database queue_driver=database 中添加以下内容

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM