Laravel - 将数据传递到电子邮件视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32500766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:19:59  来源:igfitidea点击:

Laravel - Passing data to email view

phpemaillaravellaravel-5.1

提问by Stephan-v

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

我无法通过在我的视图中使用这个变量来访问我的 email.verify 视图中的 Confirmation_code 变量:

$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.

所有其他变量,如姓名、电子邮件、密码都是可访问的,我将邮件发送方法提供给我的用户对象。

采纳答案by Mihkel Allorg

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

您是否添加'confirmation_code'$fillableUser.php 文件中的数组?

回答by Sid

Seems like your are not passing your confirmation_codeto the email template. Just save confirmation codeto some variable

似乎您没有将您confirmation_code的传递给电子邮件模板。只需保存confirmation code到某个变量

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

And in your verify.blade

而在你 verify.blade

just do {{ $confirmation_code }}

做就是了 {{ $confirmation_code }}

回答by Luca C.

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

您可以通过执行以下操作在 Mailable 视图中传递变量:

...

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

...