Laravel 5 JSON / Blade

放肆的年华 提交于 2020-01-07 02:52:11

问题


I am trying to pass a JSON to my view

With this code:

  Route::get('json', function() {
    $path = storage_path() . "/json/dish.json"; 
    $json = json_decode(file_get_contents($path), true);
    return View::make('pages.json')->withJson($json);
  });

With {{dd($json)}}

I receive this:

array:1 [▼
  "dish" => array:297 [▼
    0 => array:2 [▶]
1 => array:2 [▶]
2 => array:2 [▶]
3 => array:5 [▶]
...

When I try to display the content of my $json with:

@foreach($json["dish"] as $key => $item)
  {{$item}}
@endforeach

I get this error message:

htmlentities() expects parameter 1 to be string, array given (View: /Users/beyerdynamic/Documents/Developer/dev1/resources/views/pages/json.blade.php)

What am I doing wrong here?


回答1:


By default Laravel tries to escape any variables before input. If you want to avoid it use {!! $item !!}. However, $item is array and it will not help you to show proper values. You will get 'Array' as output. If you want to display correct data use {{ $item['your_key'] }}.



来源:https://stackoverflow.com/questions/39206896/laravel-5-json-blade

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