Laravel 5 Queue Dispatcher没有使用Property实例化

时间:2017-08-02 01:13:28

标签: php laravel asynchronous queue

所以对我来说这个注册电子邮件队列看起来很简单,我必须在某个地方遗漏某些东西!

namespace App\Jobs;     
use Illuminate\Bus\Queueable;  
use Illuminate\Queue\SerializesModels;  
use Illuminate\Queue\InteractsWithQueue;  
use Illuminate\Contracts\Queue\ShouldQueue;  
use Illuminate\Foundation\Bus\Dispatchable;  
use Mail;  
use App\Mail\EmailVerification;  
use Illuminate\Support\Facades\Log;

class SendVerificationEmail implements ShouldQueue  
{  
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;  
    protected $user;  

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $email = new EmailVerification($this->user);
        Log::alert('Showing user profile: '.json_encode($this->user));
        $user = $this->user;
        $to = "xxxx@gmail.com";
        if(isset($user->email)){
            $to = $user->email;
        }
        Mail::to( $to )->send($email);
    }
}

$ this->用户属性为空,即使它在实例化时是一个完整对象,在此处发生:

public function register(Request $request)
{
    $this->validator($request->all())->validate();
    event(new Registered($user = $this->create($request->all())));
    dd($user); //at this moment $user is everything it's supposed to be;
    dispatch(new SendVerificationEmail($user)); // it is now null??
    return view('email.verification');
}

Handle()函数是dispatch()的回调,但为什么它不会传递用户对象?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

也许您可以尝试按如下方式键入提示$user

public function __construct(User $user)
{
    $this->user = $user;
    // do a var dump here, is it populated?
}