邮件中未定义的变量::在Laravel 5中发送 [英] Undefined Variable Inside Mail::send in Laravel 5

查看:222
本文介绍了邮件中未定义的变量::在Laravel 5中发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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('mis@qdf-phils.com', '')->subject('Customer Order '.$notif[0]->order_number.' is now signed');
    });

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

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

我得到未定义的变量:notif 指向此

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

为什么我在 $ notif [0]中获得未定义的变量其中,你可以看到我的变量已经定义了上面?这是因为起始 Mail :: send 是一个单独的块,它看不到其他变量?

Why I get undefined variable in $notif[0] where as you can see my variable is already defined above? Is this because the starting Mail::send is a separate block that It can't see other variables?

推荐答案

封锁(发送电子邮件的函数)的块不具有外部块范围的可见性。

The block of the closure (your function that sends the email) has not visibility of the outside block's scope.

您想要从闭包内部访问变量,您必须使用 use 关键字将其显式传递给闭包;像这样:

So, if you want to access the variable from inside the closure, you have to explicitly pass it to the closure with the use keyword; like this:

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

有关匿名函数和关闭的更多信息,请检查此处

For more info on anonymous functions and closures check here

这篇关于邮件中未定义的变量::在Laravel 5中发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆