Respect line breaks in Laravel blade

13,001

Solution 1

You can do the escaping first, using e() and then apply nl2br():

{{ nl2br(e($user->proffile)) }}

e() is the function Blade uses when compiling triple brackets

For Laravel 5, use this: {!! nl2br(e($user->proffile)) !!}

Solution 2

Security Warning: This answer disables security features. Do not use in production!

Just {!! nl2br($user->proffile) !!}

{!! !!} instead of {{ }}.

i tested in laravel 8 blade and it worked.

Solution 3

In laravel 5 you can use {!! nl2br(e($user->proffile)) !!} but this will show the html in the browser. Doing it this way doesn't show the HTML {!! nl2br($user->proffile) !!} Last time I used it was in laravel 5.6

Share:
13,001

Related videos on Youtube

FranRP
Author by

FranRP

Updated on June 04, 2022

Comments

  • FranRP
    FranRP almost 2 years

    It is a doubt that I have tried to solve on my own using different methods, but none has given me the expected result.

    The problem comes when I save a variable of type text in the database of my project. It saves it with line breaks, in fact when I try to edit it from one of my views, it respects those jumps. The problem comes when I want to show it in a blade.php view, where all the text is without any line break.

    I have used different functions such as nl2br () or str_replace, and all they do is change the / n to the br tag for line breaks, and instead of applying those line breaks, write them on the screen next to the text.

    I do not know if this change or modification to the variable should perhaps be executed on the server and sent to the view, instead of executing in the view {{nl2br ($ user-> proffile)}} or with str_replace.

    First forgiveness for English and thanks in advance for the help

  • PiTheNumber
    PiTheNumber over 2 years
    Use nl2br(e($user->proffile)) to escape user input. Works with all Laravel 8, too.