像这样从foreach
循环中获取要显示的数据
@foreach(auth()->user()->unreadNotifications as $notification)
{{dd($notification->data)}}
{{--{{dd($notification->data['thread']['id'])}}--}}
{{--{{dd($notification->data['thread']['subject'])}}--}}
{{--{{dd($notification->data['user']['name'])}}--}}
@endforeach
所有dd
在那里显示的数据应该显示出所有错误,但是当我进行这样的for循环时。
@foreach(auth()->user()->unreadNotifications as $notification)
<a href="{{ropgpute('thread.show',$notification->data['thread']['id'])}}">
{{$notification->data['user']['name']}} commented on <strong> {{$notification->data['thread']['subject']}}</strong>
</a>
@endforeach
它给出了错误返回错误:
Undefined index: thread (View: C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\resources\views\layouts\partials\navbar.blade.php) (View: C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\resources\views\layouts\partials\navbar.blade.php) (View: C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\resources\views\layouts\partials\navbar.blade.php)
请注意,user
尚未定义
我不明白为什么dd
可以正常工作并显示数据,但是正常的foreach
循环却没有显示任何数据并返回未定义的索引线程
{{dd($notification->data)}}
输出
array:2 [▼
"thread" => array:8 [▼
"id" => 1
"subject" => "JSUpdated Updated Updated"
"thread" => "eaeaweaweaewaeawawneaklfnd/waldfwa.fwa"
"type" => "Question Updated"
"created_at" => "2019-03-14 15:34:28"
"updated_at" => "2019-03-14 15:34:38"
"user_id" => 1
"solution" => 2
]
"user" => array:6 [▼
"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"
]
]
答案 0 :(得分:0)
我注意到$notification->data['thread']
的值为"eaeaweaweaewaeawawneaklfnd/waldfwa.fwa"
。这是一个字符串。然后在您的代码中尝试$notification->data['thread']['id']
。这意味着您尝试获取一些数组值,但实际上您的数据是字符串。
答案 1 :(得分:0)
dd => dump and die
。因此,如果您包含在循环中,它将首先转储该值并中断应用程序的继续。