代码之家  ›  专栏  ›  技术社区  ›  Ying

从Laravel中的通知数据库中提取数据

  •  9
  • Ying  · 技术社区  · 7 年前

    我将通知保存到数据库中,如下所示:

    public function toDatabase($notifiable)
        {
            return [
                'from' => $this->message->name,
                'name'=> $this->message->email,
                'subject' => $this->message->subject,
                'body' => $this->message->body
            ];
        }
    

    它工作得很好。现在我想把这些数据提取到我的视图中,所以我这样做:

    @foreach ( Auth::user()->unreadNotifications as $notification)
                    <li><!-- start message -->
                        <a href="#">
                            <!-- Message title and timestamp -->
                            <h4>
                                {{ $notification->name }}
                                <small><i class="fa fa-clock-o"></i> 5 mins</small>
                            </h4>
                            <!-- The message -->
                            <p>{{ $notification->subject }}</p>
                        </a>
                    </li>
                @endforeach
    

    但它什么也没给我。那么我做错了吗?

    1 回复  |  直到 7 年前
        1
  •  17
  •   Christophvh    6 年前

    从注释中注意到,通知对象有一个数据属性,您的所有数据都存储在该属性中,以便访问它:

    更改:

    {{ $notification->name }}
    

    {{ $notification->data['name'] }}
    

    并对所有数据执行此操作。