gpt4 book ai didi

php - 将数组参数从 Controller 传递到 Mailable 类。拉维

转载 作者:可可西里 更新时间:2023-11-01 13:41:07 25 4
gpt4 key购买 nike

我正在尝试在用户成功注册后发送电子邮件。所以现在我只能在电子邮件模板中传递数据。我正在使用 Mailable 发送电子邮件。所以从我的注册 Controller 我使用这样的Mail::to('example@email.com','User Name')->send(new Verify_Email())所以我的问题是如何将数组参数传递到 new Verify_Email()Massage 构建类中。然后如何从 Verify_Email 传递到 View

RegisterController.php

public function __construct()
{
$this->middleware('guest');
}

/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
$confirmation_code = str_random(30);
$user = User::create([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'confirmation_code' => $confirmation_code
]);
$email_data = ([
'name' => $data['firstname'].' '.$data['lastname'],
'link' => '#'
]);
Mail::to('example@email.com','User Name')->send(new Verify_Email());

return $user;

}

Verify_Email.php

class Verify_Email extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*
* @return void
*/

public function __construct()
{
//
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('example@example.com')
->view('emails.verify-user');
//--------------------------> **Send data to view**
//->with([
//'name' => $this->data->name,
//'link' => $this->data->link
//]);
}

最佳答案

请遵循此方法

将输入传递给 Verify_Email 构造函数,并使用 $this-> 变量将它们传递到 View 。

Mail::to('example@email.com','User Name')->send(new Verify_Email($inputs))

然后是 Verify_Email

class Verify_Email extends Mailable {

use Queueable, SerializesModels;

protected $inputs;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct($inputs)
{
$this->inputs = $inputs;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('example@example.com')
->view('emails.verify-user')
->with([
'inputs' => $this->inputs,
]);
}

}

希望这能回答您的问题:)

关于php - 将数组参数从 Controller 传递到 Mailable 类。拉维,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39422964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com