Laravel 5中Mail :: send中的未定义变量

jackhammer013
public function postAcceptedSign($md5)
{
    $notif = CustomerOrder::where('sign_link', '=', $md5)->get();

     Mail::send('emails.signed-notif', ['order_num' => $notif[0]->order_number], function ($m) {
        $m->to('[email protected]', '')->subject('Customer Order '.$notif[0]->order_number.' is now signed');
    });

    Session::flash('alert-success', 'Order Signed.');

    return Redirect::to('home');
}

我明白了Undefined variable: notif这一点

Mail::send('emails.signed-notif', ['order_num' => $notif[0]->order_number], function ($m) {
   $m->to('[email protected]', '')->subject('Customer Order '.$notif[0]->order_number.' is now signed');
});

为什么我在$notif[0]上面已经定义了变量的地方得到了未定义的变量这是因为开始Mail::send是一个单独的块,看不到其他变量吗?

莫波

闭包块(您的发送电子邮件的函数)无法查看外部块的范围。

因此,如果要从闭包内部访问变量,则必须使用use关键字将其显式传递给闭包像这样:

Mail::send( 'emails.signed-notif', 
            ['order_num' => $notif[0]->order_number],
            function($m) use ($notif) /* here you're passing the variable */
            {
                $m->to('[email protected]', '')->subject('Customer Order'.$notif[0]->order_number.' is now signed');
            } );

有关匿名函数和闭包的更多信息,请参见此处

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章