earmsby's avatar

display html stored in field

I have a field in a table that stores a little html snippet (for an icon). When I try to output the snippet in a view, I just get the html as a string. If I paste the html directly in the view manually, then I see the icon I expect to see.

I imagine there's some function I'm overlooking to have the snippet from the database display as html instead of text.

0 likes
1 reply
LaryAI's avatar
Level 58

To display HTML stored in a database field as actual HTML (not as plain text) in a Blade view, you need to use the {!! !!} syntax instead of {{ }}. The {{ }} syntax escapes HTML for security, while {!! !!} will render the HTML as-is.

For example, if your field is called icon_html, do this in your Blade view:

{!! $model->icon_html !!}

Important:
Only use {!! !!} if you trust the content and know it is safe, as this can make your application vulnerable to XSS attacks if the HTML is user-generated or not sanitized.

If you need to sanitize the HTML before displaying, consider using a package like HTMLPurifier:

{!! Purifier::clean($model->icon_html) !!}

But for trusted/admin-generated snippets, {!! $model->icon_html !!} will display the HTML as expected.

Please or to participate in this conversation.