【问题标题】:Laravel Email Greeting show Name instead of IDLaravel 电子邮件问候显示名称而不是 ID
【发布时间】:2020-07-22 20:32:31
【问题描述】:

您好,我想询问有关 Laravel 上的电子邮件通知的问题,我已在添加此代码时制作了状态通知

public function toMail($notifiable)
{
    $leaveStatus=$this->leave->status;
    if($leaveStatus==1){
        $leaveNotify=" approved  ";
    }
    else{
        $leaveNotify=" declined ";
    }
    return (new MailMessage)
    ->subject('Leave Status')
    ->greeting('Hello '.$this->leave->user_id)
    ->line('I hope you are doing well')
    ->line('Your requested leave of' . $this->leave->type. ' type has been ' .$leaveNotify)
    ->line('Leave type: ' .$this->leave->type)
    ->line('From dt :' .$this->leave->from)
    ->line('To dt :' .$this->leave->to);

}

这非常有效,因为在休假申请的每次更改中都会发送电子邮件,所以我正在寻找的是问候的一部分,

问候('你好'.$this->leave->user_id)

它显示用户的 ID 而不是 first_name(这是名称的字段)我尝试在 user_id 之后添加 ->first_name 但随后返回错误,user_id 没有外键将它与用户表连接起来,它只是一个存储每个经过身份验证的用户的 id 的字段,但它在这里一直有效,所以我不确定这是问题所在

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    您可以使用传递给函数的可通知变量,即电子邮件被发送到的用户。

    ->greeting('Hello '.$notifiable->first_name)
    

    【讨论】:

      【解决方案2】:

      您需要在该模型中为user 添加关系。见https://laravel.com/docs/7.x/eloquent-relationships。之后通过$this->leave->user->first_name调用它

      【讨论】:

        猜你喜欢
        • 2021-08-26
        • 1970-01-01
        • 2015-07-05
        • 2011-04-08
        • 2019-03-16
        • 2021-12-27
        • 1970-01-01
        • 2020-05-11
        相关资源
        最近更新 更多