2016-09-10 97 views
2

我想在用戶成功註冊後發送電子郵件。所以現在我堅持在電子郵件模板中傳遞數據。我正在用Mailable發送電子郵件。所以從我的註冊控制器我使用這樣的Mail::to('[email protected]','User Name')->send(new Verify_Email()) 所以我的問題是如何通過陣列PARAM到new Verify_Email()按摩構建class.and所以後來如何從Verify_Email通過查看將數組參數從控制器傳遞給Mailable類。 Laravel

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 
     //]); 
} 

回答

8

請遵循本辦法

傳遞投入到Verify_Email構造和使用$這 - >變量爲p嘲笑他們的觀點。

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

} 

希望這回答你的問題:)

相關問題