Home » Php » php – Pass array parameter from controller to Mailable class. Laravel

php – Pass array parameter from controller to Mailable class. Laravel

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am trying to send email after user successfully register. so right now i am stuck to pass data in email template.I am sending email with Mailable . so from my Register Controller i using like that Mail::to('[email protected]','User Name')->send(new Verify_Email())
So my question is how to pass array param into new Verify_Email()Massage build class.and so then how to pass from Verify_Email to 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('[email protected]','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('[email protected]')
        ->view('emails.verify-user');
        //--------------------------> **Send data to view**
        //->with([            
            //'name' => $this->data->name,
            //'link' => $this->data->link
        //]); 
}
How to&Answers:

Please follow this approach

Pass the inputs to the Verify_Email constructor and use $this->variable to pass them onto the view.

Mail::to('[email protected]','User Name')->send(new Verify_Email($inputs))

and then this in 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('[email protected]')
                ->view('emails.verify-user')
                ->with([
                  'inputs' => $this->inputs,
                ]);
  }

}

Hope that answers your question 🙂