Extracting data from notification database in Laravel
Asked Answered
F

1

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?

Flanch answered 5/12, 2017 at 12:5 Comment(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?Latarsha
not it doesn't empty. i use {{ Auth::user()->unreadNotifications->count() }} and it give me the numberFlanch
unreadNotifications is part of Notifications function right?Flanch
Yes correct, sorry. What do you get when you do @php dd($notification) @endphp inside your foreach loop.Latarsha
DatabaseNotificationCollection {#962 ▼ #items: array:1 [▼ 0 => DatabaseNotification {#960 . it's too long i can't copy paste here.Flanch
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":"[email protected]","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" ]Flanch
ah it is inside a data object. So change {{ $notification->name}} to {{ $notification->data['name'] }}Latarsha
nice it works. do you want to make it an aswer so i can choose it as the right answer? :DFlanch
L
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.

Latarsha answered 5/12, 2017 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.