1

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显示的数据都应该显示没有任何错误,但是当我像这样循环时。

        @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"
]
]
4

2 回答 2

0

我注意到 is 的$notification->data['thread']"eaeaweaweaewaeawawneaklfnd/waldfwa.fwa"。这是一个字符串。然后在您的代码中尝试$notification->data['thread']['id']。这意味着您尝试获取一些数组值,但实际上您的数据是字符串。

于 2019-03-19T19:29:50.007 回答
0

dd => dump and die. 因此,如果您已包含在循环中,它将首先转储值并中断应用程序继续。

于 2019-03-20T13:27:56.570 回答