2016-09-10 10 views
2

ユーザー登録が完了した後にメールを送信しようとしています。だから私は電子メールのテンプレートでデータを渡すために立ち往生しています。私はMailableで電子メールを送信しています。ので、私の登録コントローラから私はそのMail::to('[email protected]','User Name')->send(new Verify_Email()) のように使用してだから私の質問はを見るためにVerify_Emailから渡す方法そうしてnew Verify_Email()マッサージビルドclass.andに配列のparamを渡す方法です。コントローラから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コンストラクタへの入力をパスします$ this-を使用して、このアプローチに従ってください> 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, 
       ]); 
    } 

} 

あなたの質問に答える希望:)

関連する問題