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

前端 未结 2 1657
花落未央
花落未央 2021-01-03 02:20

I know there are many questions on this topic, but none quite deal with this (as far as I could see).

I have a PHP array (which FYI, is returned via Guzzle response

相关标签:
2条回答
  • 2021-01-03 02:32

    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.

    0 讨论(0)
  • 2021-01-03 02:36

    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
    }
    
    0 讨论(0)
提交回复
热议问题