Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Mail::send in Laravel 5中未定义的变量_Php_Variables_Laravel_Laravel 5_Closures - Fatal编程技术网

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

Php Mail::send in Laravel 5中未定义的变量,php,variables,laravel,laravel-5,closures,Php,Variables,Laravel,Laravel 5,Closures,我得到了未定义变量:notif,它指向这个 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->

我得到了
未定义变量:notif
,它指向这个

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[0]
中得到未定义的变量,您可以看到我的变量已经在上面定义了?这是因为起始的
Mail::send
是一个单独的块,它看不到其他变量吗?

闭包块(发送电子邮件的函数)无法看到外部块的作用域

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

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

有关匿名函数和闭包的更多信息,请在回调中检查

,尝试使用函数($m)使用($notif){谢谢!非常好。将在计时器后接受此答案:)
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');
            } );