9

I was saved my notification into database like this:

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

it work fine. Now i want to extract that data into my view, so i do like this:

@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

but it give me nothing. so am i do it wrongly?

Ying
  • 1,282
  • 4
  • 19
  • 34
  • show us your unreadNotifications() method on users. Because i assume that is currently returning a empty collection? Or do you have a error message? – Christophvh Dec 05 '17 at 12:10
  • not it doesn't empty. i use {{ Auth::user()->unreadNotifications->count() }} and it give me the number – Ying Dec 05 '17 at 12:16
  • unreadNotifications is part of Notifications function right? – Ying Dec 05 '17 at 12:17
  • Yes correct, sorry. What do you get when you do `@php dd($notification) @endphp` inside your foreach loop. – Christophvh Dec 05 '17 at 12:21
  • DatabaseNotificationCollection {#962 ▼ #items: array:1 [▼ 0 => DatabaseNotification {#960 . it's too long i can't copy paste here. – Ying Dec 05 '17 at 12:26
  • but on #attributes: array:8 [▼ "id" => "b456af82-3517-4803-95be-847a36fc4e41" "type" => "App\Notifications\MessageRecieved" "notifiable_id" => 1 "notifiable_type" => "App\User" "data" => "{"from":ying cracker","name":"yingcracker@gmail.com","subject":"Ini adalah test.","body":"baik kita coba sent ya!!!"}" "read_at" => null "created_at" => "2017-12-05 11:03:27" "updated_at" => "2017-12-05 11:03:27" ] – Ying Dec 05 '17 at 12:28
  • ah it is inside a data object. So change {{ $notification->name}} to {{ $notification->data['name'] }} – Christophvh Dec 05 '17 at 12:31
  • nice it works. do you want to make it an aswer so i can choose it as the right answer? :D – Ying Dec 05 '17 at 12:34

1 Answers1

17

Noted from the comments The Notification object has a data attribute where all your data is stored so to access it:

change:

{{ $notification->name }}

to

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

and do this for all your data.

Christophvh
  • 12,586
  • 7
  • 48
  • 70