首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将变量推送到laravel上的通知视图中

将变量推送到laravel上的通知视图中
EN

Stack Overflow用户
提问于 2017-01-25 16:58:01
回答 2查看 4.6K关注 0票数 4

我正在从laravel 5.2迁移到5.3,当用户想要重置他的密码时,我想发送一个自定义文本。我现在明白了,laravel使用通知,默认的“主题”是硬编码在laravel内核中的。我已经有了一个视图(从5.2开始),通知可以使用自定义视图,所以我尝试了一下:

在User.php (模型)中

/**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new SendLinkMailPasswordReset($token, $this->full_name));
    }

我为“覆盖”laravel创建了我的通知"SendLinkMailPasswordReset“,这里是我的toMail()方法:

/**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->view('auth.emails.password')
                    ->with
                    (
                        [
                            'user'=> $this->full_name,
                            'token'=> $this->token,
                        ]
                    );
    }

如果我执行dd($this->full_name),它是有效的,但当我重置我的密码时,我得到Undefined variable: user

我不知道with是不是正确的方法,或者我是否想这样做是可能的。作为参考,如果我在我的sendPasswordResetNotification中这样做

public function sendPasswordResetNotification($token)
    { 
        $to=$this->email;
        $user= $this;

        Mail::send('auth.emails.password', ['user'=>$user, 'token'=>$token], function($message) use ($to) {
            $message->to($to)->subject('Reset your password'); 
        } );

    }

它起作用了。我使用通知是好的,还是我应该推送邮件?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-25 17:08:06

尝尝这个

 $user = $this->full_name;
 $token = $this->token;

 return (new MailMessage)
                ->view('auth.emails.password', compact('user','token'));

访问视图中的数据,如

{{ $user }}
{{ $token }}
票数 7
EN

Stack Overflow用户

发布于 2017-01-25 17:23:26

另一种方法是,我们可以像这样使用$viewData属性(https://laravel.com/api/5.3/Illuminate/Notifications/Messages/MailMessage.html):

$viewData=  [
                'user'=>$this->full_name, 
                'token'=>$this->token
            ];
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41847222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档