1

When ever a user post on a thread its send an notifications and then form the navagition bar there is an drop down menu where the user can see all his notifications, but when I try to open it it gives 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 an says its undefined instead

Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
HashtagForgotName
  • 651
  • 1
  • 7
  • 23

1 Answers1

4

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']

Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • is it also possible to do this in the view? in anyway – HashtagForgotName Mar 19 '19 at 14:09
  • `protected $casts = [ 'data' => 'array', ];` after adding that to the `App\Notifications\RepliedToThread.php` its still says its undefined in both the thread and the user. note The notification maps was made with `php artisan make:notification RepliedToThread ` so its out of a `model` – HashtagForgotName Mar 19 '19 at 14:31
  • You should add the `$cast` property to your model, not the notification. – Jerodev Mar 19 '19 at 14:33
  • Yes this answer solves my problem, I was looping my notifications in the vue.js component and my data was not formatted before I was doing JSON.parse to get data objects values. after reading this answer I added casts variable and add data => 'json' new its formated for me – Zeeshan Awan Oct 06 '21 at 19:14