首页 > 解决方案 > 默认 laravel 通知 --> 数据不工作

问题描述

我正在尝试使用默认的 Laravel 通知,但我不明白为什么它不显示数据。

这是我的代码

/notification/NewReply.php

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class NewReply extends Notification
{
    use Queueable;

    public $user;
    public $association;
    public $opportunity;

    public function __construct($user, $association, $opportunity)
    {
      $this->user = $user;
      $this->opportunity = $opportunity;
      $this->association = $association;
    }

    public function via($notifiable)
    {
        return ['database'];
    }

    public function toDatabase($notifiable)
    {
        return [
          'user_id' => $this->user->id,
          'association_name' => $this->association->association_name,
          'opportunity_id' => $this->opportunity->id,
          'opportunity_name' => $this->opportunity->title,
        ];
    }
}

这就是我触发通知的方式

$company->notify(new NewReply(Auth::user(),$association,$opportunity));

这是触发后保存的数据字段,没错

{"user_id":2,"association_name":"Super delter","opportunity_id":1,"opportunity_name":"Afro tenders"}

我会像这样回应,作为文档

@foreach(Auth::user()->notifications as $notification)
<p>{{$notification->data['association_name']}}</p>
@endforeach

但我什么也没得到......有人有想法吗?

标签: laravelnotifications

解决方案


推荐阅读