Display JSON - Laravel Blade

Viewed 355

is it possible to display response JSON in the blade as it is? like

{{ $collection }}

[
    {
        "id":1,
        "name":"Quam accusantium dolore qui.",
        "description":"Ipsa mollitia et rerum sint.",
        ...
    }
]

I want it well-formatted like in the above to make it more readable.

2 Answers

Yes you can use pre tag

<pre>{{ $collection }}</pre>

Also you can use JSON_PRETTY_PRINT as second param to json_encode if json is not formatted already

$data=json_decode('[
  {"id":1,"name":"Quam accusantium dolore qui.","description":"Ipsa mollitia et rerum sint."}
]');
     
    return view('welcome',['collection'=>json_encode($data, JSON_PRETTY_PRINT)]);

and in your view

  <pre >
 <code style="color: #ff12a0;">{{$collection}}</code>
  </pre>

To view json as it is you have to use html pre tag

First encode your json data like below

$collection = json_decode("Peter"=>35, "Ben"=>37, "Joe"=>43);

In html view use like this

{{ $collection }}
Related