0

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?

2
  • $item is an array. Commented Aug 29, 2016 at 12:58
  • 0 => array:2 [▶] apparently $item is also an array Commented Aug 29, 2016 at 12:59

1 Answer 1

2

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'] }}.

Sign up to request clarification or add additional context in comments.

1 Comment

My JSON file contains key like this "dish": [ { "title": "California Burger", "body": "Beefburger mit geschmolzenem Cheddar Cheese, Salat, Tomaten und Zwiebeln", "price": "7,90 €", "vendor": "Schräglage", }, { "title": "Lammkeule", "body": "aus dem Ofen mit Bratensoße, dazu Reis, dazu Beilagensalat und Fladenbrot", "price": "8,50 €", "vendor": "Taverna Sultan Saray", }] I am not sure how to itterate trough each key like (price, vendor,title,body) in my view. How can I access the "title", "body" keys?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.