2017-04-04 35 views
2

如何通過在控制器中設置如何改變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. 

謝謝的提前

回答

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; 
} 

你通過()方法可以做這樣的事情

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]; 
} 
+0

感謝Thaars的答案 –