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?

8
  • show us your unreadNotifications() method on users. Because i assume that is currently returning a empty collection? Or do you have a error message? Commented Dec 5, 2017 at 12:10
  • not it doesn't empty. i use {{ Auth::user()->unreadNotifications->count() }} and it give me the number Commented Dec 5, 2017 at 12:16
  • unreadNotifications is part of Notifications function right? Commented Dec 5, 2017 at 12:17
  • Yes correct, sorry. What do you get when you do @php dd($notification) @endphp inside your foreach loop. Commented Dec 5, 2017 at 12:21
  • DatabaseNotificationCollection {#962 ▼ #items: array:1 [▼ 0 => DatabaseNotification {#960 . it's too long i can't copy paste here. Commented Dec 5, 2017 at 12:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.