Breaking lines in text from the database

Asked

Viewed 635 times

0

I intend to load the page, the text coming from the bank, with the paragraphs and line breaks, just as it is in the bank. Below goes what I’ve tried:

View.blade.php

<div>
   <p class="cont">{{nl2br(e($post->descricao))}}</p>
</div>

Result on page

inserir a descrição da imagem aqui

What I don’t understand is the appearance of tags <br />, instead of the desired effect.

2 answers

2

To Display data without exhaust in Blade you can do with {!! $text !!} and can complement the nl2br to arrive at the expected result. It would look like this:

<div>
    <p class="cont">{!! nl2br(e($post->descricao)) !!}</p>
</div>

But be careful, as this opens gaps for XSS attack...

  • Thanks to the @Jrd tip! But I found another solution, which I will post in an answer to this question.

  • I thought it best to pass the other solution right here in the comments.

  • In the "Appserviceprovider" it adds the following in the "boot" function: Blade::setEchoFormat('nl2br(e(%s)'); In the.Lade view, you can take the data normally: {{$post->Description}}. When writing the text, do not need to enter the tag "br", just give enter to break line.

  • Another way would be: Pass the following function in the model in question (which in my case is the Post model): public Function getTextHtmlAttribute(){ Return nl2br(e($this->text),false);}. In the view it would look like this: {!! $post->Description !!}.

  • Great that you found a solution, @Daniel! But it would be better to put this solution as an answer and mark as accepted, so that there are no questions accepted by Sopt

0


I found the solution!

In the "Appserviceprovider" it adds the following in the "boot" function: Blade::setEchoFormat('nl2br(e(%s)'); In the.Lade view, you can take the data normally: {{$post->Description}}. When writing the text, do not need to enter the tag "br", just give enter to break line.

Another way would be: Pass the following function in the model in question (which in my case is the Post model): public Function getTextHtmlAttribute(){ Return nl2br(e($this->text),false);}. In the view it would look like this: {!! $post->Description !!}.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.