简体   繁体   English

Laravel - 从数组注册自定义通知通道

[英]Laravel - Register Custom Notification Channel from array

I have created a custom notification channel in Laravel, by following the documentation .我按照文档在 Laravel 中创建了一个自定义通知通道。

The custom channel class is called TeamsBotChannel , and I can easily use it by doing the below:自定义频道 class 称为TeamsBotChannel ,我可以通过执行以下操作轻松使用它:

public function via($notifiable){
  
    return [TeamsBotChannel::class];

}

Now, in my database, I store my users notification preferences in a table.现在,在我的数据库中,我将用户通知首选项存储在一个表中。 In the via method, I want to query these and return them, so users can decide what channels they want to be notified on.via方法中,我想查询这些并返回它们,以便用户可以决定他们希望在哪些渠道上得到通知。

This can look like the below:这可能如下所示:

public function via($notifiable){
    
    $channels = $notifiable->channels->toArray();
    //$channels return an array: ['email', 'slack', 'msteams']
     
}

Now my problem is, how can I link the msteams value to my custom channel TeamsBotChannel ?现在我的问题是,如何将msteams值链接到我的自定义频道TeamsBotChannel I can easily just return email and slack in the via method, but for a custom channel, I need to reference the class.我可以轻松地返回email并在via方法中slack ,但对于自定义通道,我需要参考 class。

What I ended up doing was simply just:我最终做的只是:

public function via($notifiable){
    
    $channels = $notifiable->channels;
    return $channels->map(function($item){
    
      return $item == 'msteams' ? TeamsBotChannel::class : $item;

    })->toArray();
     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM