首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有一个通知类的Laravel通知

有一个通知类的Laravel通知
EN

Stack Overflow用户
提问于 2020-01-15 07:19:39
回答 2查看 326关注 0票数 0

在我的web应用程序中,我添加了通知功能。

有3个通知类。应用程序/通知/

  1. NotifWhenLiked.php
  2. NotifyWhenStoryCommented.php
  3. NotifyWhenAuthorFollowed.php

我想用一个Notification来完成这些任务。有什么最简单的方法来解决这个问题吗?

这是一个类的代码

代码语言:javascript
运行
复制
<?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,

        ];
    }
}
EN

回答 2

Stack Overflow用户

发布于 2020-01-15 07:43:04

您可以尝试将不同的通知类型存储到数组中,例如配置文件。

代码语言:javascript
运行
复制
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'
   ]
]

您可以创建一个通用通知类,它负责处理通知内容。

代码语言:javascript
运行
复制
$whenLikedNotification = new YourCustomNotificationClass('when_liked');
$whenLikedNotification->trigger();

在构造函数中,您可以处理配置内容。

票数 0
EN

Stack Overflow用户

发布于 2020-01-15 08:07:24

我想你可以用一件事来做

创建一个事件并调用与事件相关的所有侦听器(WhenLiked, WhenStoryCommented, WhenAuthorFollowed)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59746700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档