如何通过手动更改Laravel 5.4通知

时间:2017-04-04 08:30:47

标签: laravel laravel-5.4

如何在控制器中设置通过

如果一个用户想要通知邮件而第二个用户不想通过邮件通知
那么如何设置一个用户通知发送邮件和第二个用户通知不发送邮件

$newinvoice = New NewInvoice('create a new invoice',$invoice->id);
$newinvoice->via(['database','broadcast','mail']);
Notification::send($sendToUser, $newinvoice);  

当我运行此代码时给我错误

InvalidArgumentException in Manager.php line 90: Driver [1] not supported.

提前感谢

1 个答案:

答案 0 :(得分:0)

您可以将频道作为通知构造函数的另一个参数

$newinvoice = New NewInvoice('create a new invoice',$invoice->id, 'mail');

您还可以提供一系列频道

$newinvoice = New NewInvoice('create a new invoice',$invoice->id, ['mail', 'broadcast']);

在通知类中创建一个属性

protected $_channel = null;

您的构造函数可以将通道保存到此属性

public function __construct($description, $invoiceId, $notificationChannel)
{
    $this->_channel = $notificationChannel;
}

你的via()方法可以做这样的事情

public function via($notifiable)
{
    if (!$this->_channel)
    {
        throw new \Exception('Sending a message failed. No channel provided.');
    }
    return is_array($this->_channel) ? $this->_channel : [$this->_channel];
}