我的代码是这样的:
public function handle(UserRegisteredEvent $event)
{
$data = $event->data;
$name = $data['name'];
$email = $data['email']; //tri@gmail.com
Mail::send('emails.welcome', ['name' => $name], function($m){
$m->to($email)
->subject('Transaction Details');
});
}
执行时,会出现如下错误:
Undefined variable: email
答案 0 :(得分:2)
use
变量,您必须使用public function handle(UserRegisteredEvent $event)
{
$data = $event->data;
$name = $data['name'];
$email = $data['email']; //tri@gmail.com
Mail::send('emails.welcome', ['name' => $name], function($m) use ($email) {
$m->to($email)
->subject('Transaction Details');
});
}
关键字将其显式传递给闭包函数,如下所示:
$insert
答案 1 :(得分:1)
使用函数闭包时,您需要使用use
来使用内部变量。
试试这个:
Mail::send('emails.welcome', ['name' => $name], function($m) use ($email) {
$m->to($email)
->subject('Transaction Details');
});