所以我的邮件功能可以给用户发送邮件。然而,我希望能够用他们注册时输入的用户名发送邮件。例如’Hello “john” ‘,其中john是输入的名字。我有以下代码:
我有以下代码: RegisterController.php:
protected function create(array $data)
{
Mail::to($data['email'])->send(new WelcomeMail());
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
欢迎你,布莱恩
@component('mail::message')
Welcome to the Hotel Booking System {{$data->name}}
The body of your message.
Thanks,<br>
{{ config('app.name') }}
@endcomponent
现在在welcome.blade.php中,我收到了Undefined Variable $name的错误信息。我如何使用这两段代码来解决这个问题。
解决方案:
你必须将数据传入 WelcomeMail()
Mail::to($data['email'])->send(new WelcomeMail($data['name']));
WelcomeMail类内部
public $name;
public function __construct($name)
{
$this->name = $name;
}
你可以在你的markdown中访问名称变量。
Welcome to the Hotel Booking System {{$name}}
如果你想把整个$data数组传递给构造函数
send(new WelcomeMail($data);
你可以这样做
public $data;
public function __construct($data)
{
$this->data = $data;
}
或
public $name, $email;
public function __construct($data)
{
$this->name = $data['name];
$this->email = $data['email];
}
您也可以分别传递每个值
send(new WelcomeMail($data['name'], $data['email']);
public function __construct($name, $email)
{
$this->name = $name;
$this->email = $email;
}