Laravel уведомление с одним классом уведомлений - PullRequest
0 голосов
/ 15 января 2020

В своем веб-приложении я добавил функции уведомлений.

Есть 3 класса уведомлений. Приложение / Уведомления /

  1. NotifWhenLiked. php
  2. NotifyWhenStoryCommented. php
  3. 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,

        ];
    }
}

Ответы [ 2 ]

0 голосов
/ 15 января 2020

Я думаю, вы можете использовать событие, чтобы сделать это

Создать событие и вызвать всех слушателей (WhenLiked, WhenStoryCommented, WhenAuthorFollowed), связанных с событием

0 голосов
/ 15 января 2020

Вы можете попытаться сохранить различные типы уведомлений в массиве, например, в файле конфигурации.

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();

В конструкторе вы можете работать с настройками.

...