如何将Laravel通知与通知分组?

问题描述

我在使用Laravel通知时遇到一个问题,我想调用一个分组的通知,而没有多次重复相同的通知

例如,在电子邮件捕获中,如果项目名称及其翻译不存在,Laravel会注册数据,并且在向我发送通知时存在。我不想重复这个主题,只做一个计数器,然后像这样向我展示消息。

enter image description here

我正在占用事件和侦听器,但无法在视图中将它们分组。

控制器

class TranslateEvent
{
    use dispatchable,InteractsWithSockets,SerializesModels;

    public $translate;
    
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($translate)
    {
        $this->translate = $translate;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

事件

class TranslateNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(TranslateProject $translate)
    {
        $this->translate = $translate;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'uuid'      => $this->translate->id,'id'        => 'Traduccion','name'      => $this->translate->translate,'route'     => route('translate.index'),'created_at'=> Carbon::parse($this->translate->created_at)->diffForHumans()
        ];
    }
}

通知

class TranslateListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
        User::withRole('admin')
            ->each(function(User $user) use ($event){
                Notification::send($user,new TranslateNotification($event->translate));
            });
    }
}

监听器

    public static function groopNotification(){
        $amount = 0;
        foreach (Auth::user()->unreadNotifications as $notification) {
            if ($notification->data['id'] == 'Traduccion') {
                $amount++;
            }
        }
        return $amount;
    }

用户

                           @foreach (Auth::user()->unreadNotifications()->take(4)->get() as $notification)

                           @if( $notification->data['id'] == 'Traduccion')
                           <a href="{{ $notification->data['route']}}" class="iq-sub-card">
                              <div class="media align-items-center">
                                 <div class="media-body ml-3">
                                    <h6 class="mb-0 ">There are {{ \App\User::groopNotification() }} translations without projects.</h6>
                                    <small
                                       class="float-right font-size-12">{{ $notification->created_at->diffForHumans() }}</small>
                                 </div>
                              </div>
                           </a>
                           @else

                           @endif
                           @endforeach

HTML

{{1}}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)