Home > Net >  How to include line breaks in textarea tag in create post in laravel?
How to include line breaks in textarea tag in create post in laravel?

Time:09-22

I have the following code and I just noticed when I post a post with two paragraphs the line break break between the paragraphs is ignored. The output is one long paragraph without any line break. Is there a way to fix that.

<div class="contact-form">
    <form action="{{ route('posts.store') }}" method="post" enctype="multipart/form-data">
        @csrf
        <!-- Body-->
        <label for="body"><span>Body</span></label>
        <textarea id="body" name="body">{{ old('body') }}</textarea>
        @error('body')
        <p>{{ $message }}</p>
        @enderror
        <!-- Button -->
        <input type="submit" value="Submit" />
    </form>
</div>

CodePudding user response:

Use PHP's nl2br() function in combination with outputting HTML in Laravel:

<textarea id="body" name="body">{!! nl2br(old('body')) !!}</textarea>

This will display the old('body') input, keeping HTML formatting and spaces intact.

CodePudding user response:

You could apply a CSS style to your text area to preserve the line breaks:

<textarea id="body" name="body" class='preserveLines'>{{ old('body') }}</textarea>
.preserveLines {
    white-space: pre-wrap;
}
  • Related