【发布时间】:2020-01-15 07:19:39
【问题描述】:
在我的网络应用程序中,我添加了通知功能。
有 3 个通知类。 应用/通知/
- NotifWhenLiked.php
- NotifyWhenStoryCommented.php
- NotifyWhenAuthorFollowed.php
我想用一个通知类来完成这些任务。有没有最简单的方法来解决这个问题?
这是一个类的代码
<?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,
];
}
}
【问题讨论】:
-
如果所有类都执行类似的功能,那么只保留一个类并删除其他类。无论如何,您都必须更改手动发送这些通知的所有代码。