Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

behnampmdg3's avatar

Blade nl2br and a CSS question

Hi;

I got 2 questions.

What's the best practice nl2br for Blade?

I found something that works but I am not sure if this is correct or not. It does escape tags and adds line breaks. Thoughts?

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

2 - What is the proper way of placing Edit And Delete button on the same line? Please note delete is inside of a form (obviously).

<!-- Blog Post -->
  <div class="card mb-4">
    <div class="card-body">
      <h2 class="card-title">{{ $post->title }}</h2>
      <p class="card-text">{{ $post->body }}</p>
    </div>
    <div class="card-footer text-muted">
      {{ $post->created_at->diffForHumans() }}
      <a href="/users/{{ $post->user->id }}">{{ $post->user->name }}</a>

      @if(Auth::id()===$post->user_id)
      <a href="/posts/{{ $post->id }}/edit" class="btn btn-secondary btn-sm">Edit &rarr;</a> 
      <form method = "POST" action="/posts/{{ $post->id }}">
        @csrf
        @method('DELETE')
        <input type="submit" class="btn btn-danger btn-sml" value="Delete">
      </form>  
      @endif
    </div>
  </div>
<!---->
0 likes
2 replies
Lunah's avatar
  1. This is fine, however, you can clean this up a little by extracting the nl2br() functionality to your model.
// Post.php

function getFormattedBodyAttribute()
{
    return nl2br($this->body);
}

Then you can simply do $post->formatted_body in your view.

You can, of course, call formatted_body anything you like.

Second... Impossible to say without seeing your CSS. Make sure your form is set to inline.

behnampmdg3's avatar

Hey Lunah;

Thanks, I like your function.

However, I rather not keep it the main model.

What would be the best practice? A better place to keep this code?

Is this where repository or service container comes handy?

Thanks

Please or to participate in this conversation.