我有一个问题,我无法将变量放入 laravel 的 Mail::send() 函数中。请看下面的代码:
$first_name = $request->input('first_name'),
$email = $request->input('email'),
//Create account
User::create([
'first_name' => $first_name,
'last_name' => $request->input('last_name'),
'email' => $email,
'password' => bcrypt($request->input('password')),
]);
//Send email to user
Mail::send('emails.test', ['fname' => $first_name], function($message)
{
$message->to($email)
->subject('Welcome!');
});
return redirect()
->route('home')
->with('info', 'Your account has been created and an authentication link has been sent to the email address that you provided. Please go to your email inbox and click on the link in order to complete the registration.');
由于某种原因,代码在发送电子邮件时中断,因为我收到错误并将数据发送到数据库。为什么此后无法再访问该变量?
如有任何帮助,我们将不胜感激。谢谢
最佳答案
Closures may also inherit variables from the parent scope. Any such variables must be passed to the
use
language construct.
来源:http://php.net/manual/en/functions.anonymous.php
换句话说,$email
必须像这样继承:
Mail::send('emails.test', ['fname' => $first_name], function($message) use ($email)
{
$message->to($email)
->subject('Welcome!');
});
注意:在第一行使用($email)
。
关于创建帐户后无法访问 PHP Laravel 5 用户类属性以发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33457624/