通过嵌套的Json对象Laravel循环出错

时间:2018-08-28 00:08:40

标签: json laravel laravel-blade

我从api返回了json数据,并希望使用Blade进行输出。

 9 => {#670 ▼
    +"id": 10300
    +"name": "Fallout 3: Mothership Zeta"
    +"slug": "fallout-3-mothership-zeta"
    +"url": "https://www.igdb.com/games/fallout-3-mothership-zeta"
    +"created_at": 1431649834920
    +"updated_at": 1532126985619
    +"summary": "Defy hostile alien abductors and fight your way off of the massive Mothership Zeta, orbiting Earth miles above the Capital Wasteland. Mothership Zeta takes Fall ▶"
    +"collection": 3
    +"rating": 77.763623864376
    +"popularity": 1.3333333333333
    +"total_rating": 77.763623864376
    +"total_rating_count": 29
    +"rating_count": 29
    +"game": 15
    +"games": array:10 [▶]
    +"tags": array:4 [▶]
    +"developers": array:1 [▶]
    +"publishers": array:1 [▶]
    +"category": 1
    +"player_perspectives": array:1 [▶]
    +"game_modes": array:1 [▶]
    +"themes": array:1 [▶]
    +"genres": array:2 [▶]
    +"first_release_date": 1249257600000
    +"platforms": array:3 [▶]
    +"release_dates": array:3 [▶]
    +"screenshots": array:7 [▶]
    +"cover": {#681 ▼
      +"url": "//images.igdb.com/igdb/image/upload/t_thumb/btawsi7cbgcmqlw67tpn.jpg"
      +"cloudinary_id": "btawsi7cbgcmqlw67tpn"
      +"width": 1061
      +"height": 1158
    }
  }

我希望得到的是我尝试过的Cover URL,并遍历所有json数据,并将上述10项返回到其中

   @foreach ($game as $games)
                  <div class="col-md-2">
                      <img src="" alt="">
                      <p>{{ $games->name}}</p>
                      @foreach ($games->cover as $cover)
                          <p>{{$cover['url']}}</p>
                      @endforeach
                  </div>
              @endforeach

当前,这就是我循环中的内容,我收到此错误

非法的字符串偏移量'url'

2 个答案:

答案 0 :(得分:0)

尝试一下:

{{ $arr[9]->id }}
{{ $arr[9]->name }}
{{ $arr[9]->cover->url }}

注意,我假设这是第10个元素的较大数组的名称为$arr。将该名称替换为代码中的名称。

答案 1 :(得分:0)

在循环中,将$games更改为$game

@foreach($games as $game)
    <div class="col-md-2">
        <img src="" alt="">
        <p>{{$game->name}}</p>
        <p>{{$game->cover->url}}</p> 
    </div>
@endforeach