我正在尝试在用户成功注册后发送电子邮件。目前,我遇到了在电子邮件模板中传递数据的困境。我使用Mailable发送电子邮件。所以在我的Register Controller中,我像这样使用:Mail::to('example@email.com','User Name')->send(new Verify_Email())
。那么我的问题是如何将数组参数传递到new Verify_Email()
消息构建类中,并从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;
}
验证电子邮件.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
//]);
}
->with(['line' => $this->line])
,那么在你的视图中,只需这样做{{ $line }}
。希望这可以帮助你。 - prateekkathal