通过User的notify发送通知怎么样可以动态切换收件人?

目前情况是通过$user->notify()来发送通知,这种默认都是User模型的email字段,在社区看到可以在User模型重写routeNotificationForMail方法来指定收件人字段,但这种好像只能固定。

我现在有一个需求:用户表里面除了email字段,还有一个联系人邮箱contact_email字段,需要可以给这两个都能发邮件通知:
User.php

protected $sendToContactEmail = null;

public function sendToContactEmail($email=null)
{
    $this->sendToContactEmail = $email;
    return $this;
}

public function routeNotificationForMail()
{
    return $this->sendToContactEmail ?? $this->email;
}

尝试通过$user->sendToContactEmail($mail)->notify()User.php中新增一个方法,来改变这个指定的email字段,但是好像没起作用,默认还是发的用户的email字段,这种要怎么传参来动态改变呀?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 6

发送该说有事件的吧,在发送事件前修改该说可以

2年前 评论
ljheisenberg (楼主) 2年前
deatil (作者) 2年前
ljheisenberg (楼主) 2年前

感觉按理说是没问题的,不行就试试这样
Notification::route(‘mail’, $email)->notify();

2年前 评论
protected bool $sendToContactEmail = false;

public function sendToContactEmail(bool $email=true)
{
    $this->sendToContactEmail = $email;
    return $this;
}

public function routeNotificationForMail()
{
    return $this->sendToContactEmail ? $this->contact_email : $this->email;
}

文档里说是对应的字段,就一直用字段吧

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!