Laravel - Passing data to email view

11,930

Solution 1

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

Solution 2

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 }}

Solution 3

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

...

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

...
Share:
11,930
Stephan-v
Author by

Stephan-v

Hi there I love programming, designing and coming up with fresh new ideas. My goto programming framework is Laravel complemented with Vue.js. I'm a quick learner and I love getting up to date on the latest technology. Helping people out with their programming conundrums is something I love to do.

Updated on June 14, 2022

Comments

  • Stephan-v
    Stephan-v almost 2 years

    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.

  • Stephan-v
    Stephan-v over 8 years
    Damn. I found out this was the case indeed. But I am wondering how come you sometimes get a mass assignment exception error but in this case it didn't give any error?
  • Mihkel Allorg
    Mihkel Allorg over 8 years
    @Stephan-v I think mass assignment exception error comes when you're trying to create the new model object.