Looping PHP Nested Arrays - Extract values into Blade Views (Laravel)

孤街醉人 提交于 2019-11-30 14:45:21

You may try this:

@foreach ($users['spirits'] as $user)
 {{ $user["id"] }}
 {{ $user["name"] }}
@endforeach

It's better to check the returned result in your controller before you send it to the view using something like this so there will be no errors in your view:

$users = 'Get it from somewhere...';
if(!$users['error']) {
    return View::make('users')->with('users', $users);
}
else {
    // Show an error with a different view
}

in case your users are always stored in the spirits-key of your $users variable you simply could modify your @foreach-loop as follow:

@foreach ($users['spirits'] as $user)
    {{ $user['id'] }}
    {{ $user['name'] }}
@endforeach

Otherwise you could edit your return value from the controller. That means you simply could change the line:

return View::make('users')->with('users',$users);

to

return View::make('users')->with('users',$users['spirits']);

In this case you don't have access to your error-key.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!