Laravel:通过电子邮件发送用户名

时间:2020-04-23 11:22:09

标签: laravel

因此,我的电子邮件功能可以向用户发送电子邮件。但是,我希望能够使用他们在注册时输入的用户名发送电子邮件。例如'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']),
    ]);

}
}

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的错误消息。我将如何使用两段代码解决此问题。

1 个答案:

答案 0 :(得分:1)

您将必须将数据传递到 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;
}