0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

詳細画面で、説明文などを「改行」させる方法

Posted at

はじめに

Laravelなどでtextareaに入力されたテキストを、show画面(表示画面)で改行を反映させたい場合は、
Bladeで改行文字(\n)をHTMLの
に変換する必要がある。

やり方:nl2brを使う

{!! nl2br(e($yourModel->your_text_column)) !!}

解説

・e(...):HTMLエスケープ(XSS対策)
・nl2br(...):改行(\n)をbrタグに変換
・{!! ... !!}:HTMLタグとして表示({{ }} だとタグが無効になる)

DBにこんな値が入ってたとする:

こんにちは
これはテストです。
よろしくお願いします。

表示部分に下記を使えば:

{!! nl2br(e($post->body)) !!}

表示はこうなります:

こんにちは<br>
これはテストです。<br>
よろしくお願いします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?