2

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.

1
  • view('your-view')->with('collection ', json_decode($collection , true)); Commented Jun 21, 2021 at 3:52

2 Answers 2

5

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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 }}

Comments

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.