首页 > 解决方案 > Laravel 根据发送方法向不同的通知发送通知

问题描述

我有一个应用程序可以向管理员发送有关某些用户操作的通知。一些管理员应通过 SMS 通知,而其他管理员应仅通过数据库通知。做这个的最好方式是什么?使用两个通知还是只使用一个?我没有找到通过通知中的通知方法更改通知的任何方法。

有什么建议吗?

标签: laravelnotificationslaravel-notification

解决方案


它可以与 If 语句一起使用

class yourNotifName extends Notification
{
    use Queueable;
    private $role;
    private $notif;

    public function __construct(User $user)
    {
        $this->role= $user->role;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        if($this->role == "admins") {
            $this->notif = ['sms'];
        } else {
           $this->notif = ['database'];
        }
        return $this->notif;
    }
}

推荐阅读