Stephan-v
Stephan-v

Reputation: 20289

Laravel - Passing data to email view

I can not access the confirmation_code variable in my email.verify view by using this variable in my view:

$user->confirmation_code

Shouldn't this be accessible when I assigned the array items like this? What am I overlooking?

$user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'confirmation_code' => str_random(30)
        ]);

        Mail::send('emails.verify', ['user' => $user], function ($m) use ($user) {
            $m->to($user->email, $user->name)->subject('Email verificatie');
        });

All the other variables like, name, email, password are accessible and I am giving the mail send method my user object.

Upvotes: 2

Views: 12845

Answers (3)

Luca C.
Luca C.

Reputation: 12564

You can pass variables in Mailable views, by doing this:

...

public function build()
{
    return $this->view('emails.viewname')->with(['explicit_variable' => $some_value]);
}

...

Upvotes: 1

Sid
Sid

Reputation: 5833

Seems like your are not passing your confirmation_code to the email template. Just save confirmation code to some variable

 Mail::send('emails.verify', ['user' => $user, 'confirmation_code' => $yourConfirmationCodevariable ], function($m){
                        $$m->to($user->email)->subject('Transaction Details');
                    });

And in your verify.blade

just do {{ $confirmation_code }}

Upvotes: 3

Mihkel Allorg
Mihkel Allorg

Reputation: 989

Did you add 'confirmation_code' to the array $fillable in the User.php file?

Upvotes: 2

Related Questions