从控制器传递数组参数到可邮寄类。Laravel

11

我正在尝试在用户成功注册后发送电子邮件。目前,我遇到了在电子邮件模板中传递数据的困境。我使用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
        //]); 
}
1个回答

18
请按照以下步骤操作: 将输入传递给“ Verify_Email 构造函数”,并使用 $this->variable 将它们传递到视图中。
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,
                ]);
  }

}

希望这回答了你的问题 :)


@Marz 你是什么意思? - prateekkathal
1
@Marz 不确定你在所有这些单行注释中暗示了什么,但我建议提供更多信息,这样我才能提供帮助。 - prateekkathal
我知道这是一个3年前的帖子,但为了澄清上面的示例,我仍然可以在句子中使用上面那个$inputs的内容吗?在构建的这行代码中,我有一行代码,它说:“你被拒绝的原因是因为这个: '数据在这里'”,我已经建立了一个数据库,可以输入原因,并将其提取并用于这个句子,您能帮我吗?谢谢。 - Aia's Blog
@Aia的博客 我不确定你的代码具体是什么样子的。但是,是的,你应该能够将任何内容发送到视图中。只需确保在视图文件中使用相同的变量来显示它。所以,如果你这样做 ->with(['line' => $this->line]),那么在你的视图中,只需这样做 {{ $line }}。希望这可以帮助你。 - prateekkathal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接