使用Mail时,Laravel视图中的未定义变量

时间:2016-12-01 10:28:50

标签: laravel-5.2

尝试使用Laravel 5.2发送验证邮件时,出现错误:

  

未定义变量:confirmation_code(查看:   C:\ XAMPP \ htdocs中\ laravel \资源\视图\电子邮件\ verify.blade.php)

我的代码如下所示:

Controller.php这样:

     public function postSignup(Request $request){                                                                               
       $this->validate($request,[
       'email'=>'required|unique:users|email',
       'name'=>'required|max:50|min:3',
       'password'=>'required|min:6',
       'con-password'=>'required|same:password',
               ]);

    $confirmation_code=['code'=>str_random(20)];
    $name = $request->input('name');

    Mail::send('email.verify',$confirmation_code,function($message)
        use($request,$name){
        $message->to($request->input('email'),$name)
                ->subject('Verify Your Email Address');
    });


    User::create([
            'email'=>$request->input('email'),
            'name'=>$request->input('name'),                
            'password'=>bcrypt($request->input('password'))                          
        ]);

    return redirect()->back()->with('info','Congratulation you have been successfully registered.Please check your email for verification');
    } 

Mail.verify.blade.php:

    <h2>Verify Your Email Address</h2>

    <div>
        Thanks for creating an account with the verification demo app.
        Please follow the link below to verify your email address
        {{ URL::to('register/verify/'.$confirmation_code) }}.<br/>

    </div>

</body>

1 个答案:

答案 0 :(得分:1)

试试这个:

Mail::send('email.verify', compact('confirmation_code'), function ($message) use($request, $name) {
    $message->to($request->input('email'),$name)
            ->subject('Verify Your Email Address');
});

它失败的原因是Laravel视图接受一个关联数组作为它们的数据,因此它可以使用键作为变量名称将它们转换为变量,并将它们与相应的值匹配。

compact做的是将变量变成一个关联数组,变量的名称作为其键(与Laravel视图的作用相反)。