问题描述
因此,我的电子邮件功能可以将电子邮件发送给用户.但是我希望能够使用他们在注册时输入的用户名发送电子邮件.例如,"Hello"john"',其中john是输入的名称.我有以下代码:
So my email functionality can send emails to users. However i want to be able to send emails with the username that they enter when they register. For example 'Hello "john" ' in which john is the name entered. I have the following code:
RegisterController.php:
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']),
]);
}
}
Welcome.blade.php
Welcome.blade.php
@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中,我正在收到未定义变量$ name的错误消息.我将如何使用两段代码解决此问题.
Now in the welcome.blade.php is where i am receiving the error message of Undefined Variable $name. How would i fix this using the two pieces of code.
推荐答案
您将必须将数据传递到 WelcomeMail()
You will have to pass data into WelcomeMail()
Mail::to($data['email'])->send(new WelcomeMail($data['name']));
内部WelcomeMail类
Inside WelcomeMail class
public $name;
public function __construct($name)
{
$this->name = $name;
}
您不能在markdown内访问名称变量
Than you can access name variable inside your markdown
Welcome to the Hotel Booking System {{$name}}
如果您要将整个$ data数组传递给构造函数
If you want to pass whole $data array into constructor
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];
}
您还可以分别传递每个值
You can also pass each value separately
send(new WelcomeMail($data['name'], $data['email']);
public function __construct($name, $email)
{
$this->name = $name;
$this->email = $email;
}
这篇关于Laravel:通过电子邮件发送用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!