问题
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