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