[英]Laravel 5.3 - Single Notification File for Different User Collections?
Given 100
followers, once I complete a todo, a notification is fired.给定
100
关注者,一旦我完成了一个待办事项,就会触发一条通知。 The issue is 80
of them want email only notification, the other 20
want sms only notification.问题是其中
80
只需要电子邮件通知,另外20
只需要短信通知。 Does that mean I need to use 2 different notification calls like this:这是否意味着我需要使用 2 个不同的通知调用,如下所示:
Notification::send($emailOnlyUsers, new TodoCompletedEmail($todo));
which has only the mail channel, and then:它只有邮件通道,然后:
Notification::send($smsOnlyUsers, new TodoCompletedSms($todo));
which has only the sms channel?哪个只有短信频道? Or is it possible to have the logic of
$emailOnlyUsers
and $smsOnlyUsers
in the TodoCompleted Notification file where both channels are listed together to handle the different channels for different users in one file?或者是否有可能在 TodoCompleted 通知文件中包含
$emailOnlyUsers
和$smsOnlyUsers
的逻辑,其中两个频道一起列出来处理一个文件中不同用户的不同频道? Something like this:像这样的东西:
$user->notify(new TodoCompletedEmail($todo));
I ask because I would rather do it in one file with different channels, but I don't think I can since Laravel's Notification expects a passed collection of users (in this situation there are really 2 user collections), but more channels/different collections are possible and it would suck to have to create a new notification file for each channel/user collection for the same TodoCompleted
notification.我问是因为我宁愿在具有不同频道的一个文件中执行此操作,但我认为我不能,因为 Laravel 的通知需要传递的用户集合(在这种情况下实际上有 2 个用户集合),但是更多的频道/不同的集合是可能的,并且必须为相同的
TodoCompleted
通知为每个频道/用户集合创建一个新的通知文件会很TodoCompleted
。 Can someone shed some light on whether it is possible to have it in a single file and if so how?有人可以说明是否可以将它放在一个文件中,如果可以,如何?
What about something like this?这样的事情怎么办?
foreach($users as $user)
$user->notify(new TodoCompleted($todo));
Then in your TodoCompleted Class然后在你的 TodoCompleted 类中
private $todo;
/**
* Create a new notification instance.
*
* @param $todo
*/
public function __construct($todo)
{
$this->todo = $todo;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
// Change "prefer_sms" to whatever logic you're using to determine if the user should be sent sms or email.
if($notifiable->prefers_sms)
return ['sms', 'database'];
return ['mail', 'database'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.