Search code examples
laravellaravel-notificationlaravel-5.8

How to get data out of the notifications? [Error retrieving data] Laravel 5.8


When ever a user post on a thread its send an notifications and then form the navigation bar there is an drop down menu where the user can see all his notifications, but when I try to open it, I get an error:

Undefined index: thread (View: C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\resources\views\layouts\partials\notification\replied_to_thread.blade.php) (View: C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\resources\views\layouts\partials\notification\replied_to_thread.blade.php) (View: C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\resources\views\layouts\partials\notification\replied_to_thread.blade.php) (View: C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\resources\views\layouts\partials\notification\replied_to_thread.blade.php)

While if I do an dd($notification), I get this data:

#attributes: array:8 [▼
"id" => "c055bf7e-8aa0-41d6-b188-d73073dbfefb"
"type" => "App\Notifications\RepliedToThread"
"notifiable_type" => "App\User"
"notifiable_id" => 1
"data" => "{"thread":{"id":2,"subject":"22JSUpdated Updated Updated","thread":"eaweaewarea kejwabnejk.wabe;beh;lkwabhel.kwajewaeaweawe","type":"Question Updated","created_ ▶"
"read_at" => null
"created_at" => "2019-03-19 13:28:11"
"updated_at" => "2019-03-19 13:28:11"
]
#original: array:8 [▼
"id" => "c055bf7e-8aa0-41d6-b188-d73073dbfefb"
"type" => "App\Notifications\RepliedToThread"
"notifiable_type" => "App\User"
"notifiable_id" => 1
"data" => "{"thread":{"id":2,"subject":"22JSUpdated Updated Updated","thread":"eaweaewarea kejwabnejk.wabe;beh;lkwabhel.kwajewaeaweawe","type":"Question Updated","created_ ▶"
"read_at" => null
"created_at" => "2019-03-19 13:28:11"
"updated_at" => "2019-03-19 13:28:11"
]

And in the navbar I am trying to access the notification data like this:

@foreach(auth()->user()->unreadNotifications as $notification)
<a href="{{route('thread.show',$notification->data['thread']['id'])}}">

    {{$notification->data['user']['name']}} commented on <strong> 
{{$notification->data['thread']['subject']}}</strong>
</a>
@endforeach

but its says ['thread']['id'] is undefined while in the dd I can see that in the `array:8 is

"data" => "{"thread":{"id":2,"subject":"22JSUpdated Updated Updated","thread":"eaweaewarea kejwabnejk.wabe;beh;lkwabhel.kwajewaeaweawe","type":"Question Updated","created_at":"2019-03-19 10:56:58","updated_at":"2019-03-19 10:56:58","user_id":1,"solution":null},"user":{"id":1,"name":"johndoe","email":"johndoe@example.com","email_verified_at":null,"created_at":"2019-03-14 15:34:15","updated_at":"2019-03-14 15:34:15"}} 

But why can I not find the id of the thread and says it's undefined instead?


Solution

  • data is a string containing a json format. If you don't tell PHP this information, PHP will just interpret this as a string, which has no keys.

    So, you will have to parse the json before using the keys:

    $data = json_decode($notification->data);
    echo $data->thread->id;
    

    Update: I supose you are getting these values using Eloquent? If so, you can let eloquent parse these json strings for you.

    Just define a $casts property on your model and add the property.

    protected $casts = [
        'data' => 'array',
    ];
    

    Now you can use the property directly: $notification->data['thread']['id']