在我的web应用程序中,我添加了通知功能。
有3个通知类。应用程序/通知/
我想用一个Notification来完成这些任务。有什么最简单的方法来解决这个问题吗?
这是一个类的代码
<?php
namespace App\Notifications;
use App\Http\Resources\Users;
use App\Model\User;
use App\Model\Story;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NotifyWhenLiked extends Notification implements ShouldQueue
{
use Queueable;
public $user;
public $story;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Story $story, User $user)
{
$this->user = $user;
$this->story = $story;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database','broadcast'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toDatabase($notifiable)
{
return [
'notification' => "<strong>".$this->user->name."</strong>". ' liked your story '. "<strong>".$this->story->title."</strong>",
'Storylink' => '/story/'.$this->story->url_key,
'Userlink' => '/a/'.$this->user->profile->username
];
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'notification' => $this->user->name. ' liked your story '. "<strong>".$this->story->title."</strong>",
'username' => $this->user->profile->username,
];
}
}发布于 2020-01-15 07:43:04
您可以尝试将不同的通知类型存储到数组中,例如配置文件。
return [
'when_liked' => [
'notification_text' => '%s liked your story %s',
'story_link' => '/story/%s'
],
'when_commented' => [
'notification_text' => '%s commented on your story %s',
'story_link' => '/story/%s'
]
]您可以创建一个通用通知类,它负责处理通知内容。
$whenLikedNotification = new YourCustomNotificationClass('when_liked');
$whenLikedNotification->trigger();在构造函数中,您可以处理配置内容。
发布于 2020-01-15 08:07:24
我想你可以用一件事来做
创建一个事件并调用与事件相关的所有侦听器(WhenLiked, WhenStoryCommented, WhenAuthorFollowed)
https://stackoverflow.com/questions/59746700
复制相似问题