Skip to content
Advertisement

Can you read JSON into Laravel and turn into DOM element?

I have a JSON file with text. I want to print that out into a div and have an anchor tag inside it.

I have the JSON –

    {
        "text" : "A bunch of text here with a <a href="#">link</a>"
    }

Inside my controller, I decode the JSON

    $json = Storage::disk('local')->get('settings.json');
    $json = json_decode($json, true);
    return $json;

Inside my view

    <p>{{ $text }}</p>

At the moment the p tag would just echo the response as a string

A bunch of text here with a <a href=”#”>link</a>

How can turn this into html? Is that even a thing?

Advertisement

Answer

Basically Laravel does html entity encoding when you print something usingĀ {{ $text }}.

In order to not to encode HTML elements, you have to use

{!! $text !!} 

This can be also used for web page rendering as well for many other use case.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement