Respect line breaks in Laravel blade

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Respecting Line Breaks in Laravel Blade: Fixing Multi-line Text Display It is a common frustration when dealing with data retrieved from a database. As developers, we often encounter scenarios where data stored in a standard text field retains its internal line breaks (`\n` characters), but when rendered in an HTML context via a Blade view, these breaks are lost, resulting in a flat block of text. This post addresses the specific challenge: how to correctly display multi-line text retrieved from the database within Laravel Blade views, ensuring that the original formatting—the line breaks—is respected as visual line breaks in the final output. --- ## The Problem: Text vs. HTML Rendering The core issue lies in the difference between a plain string and formatted HTML. When you store text in your database (e.g., a `TEXT` or `VARCHAR` column), it stores characters exactly as they are entered. If a user inputs text with a manual line break, the database stores the newline character (`\n`). When Laravel fetches this data and outputs it directly into a Blade view using standard echo syntax (`{{ $variable }}`), the browser treats all whitespace (including `\n`) as a single space, collapsing the lines together. You correctly noted that attempts to force HTML line breaks using functions like `nl2br()` or `str_replace('\n', '
')` often fail because they operate purely on string manipulation. They convert the newline character into an HTML break tag, but without proper context or handling, the browser doesn't render it as a line break unless wrapped in appropriate HTML structure. ## The Solution: Process Data on the Server Side The fundamental principle in any robust application is to separate concerns. Presentation logic (how text looks) should ideally be handled by the view layer (Blade), while data preparation and transformation should happen on the server side, typically within your Controller or Eloquent Model. This ensures that the view remains clean and focused solely on rendering, regardless of the complexity of the underlying data. We need to transform the stored newline characters into HTML line break tags *before* the data is sent to the view. ### Step 1: Transformation in the Model or Controller Instead of trying complex string manipulation within the Blade file, let's handle this transformation where the data originates. If you are using Eloquent, you can implement accessor methods or simply handle the formatting when retrieving the data. Consider a scenario where you fetch a user profile that contains a multi-line biography: ```php // Example in a Controller method (or Eloquent Model) use App\Models\User; public function showProfile(User $user) { // Fetch the raw text from the database $rawBio = $user->biography; // Assume this contains "\n" characters // Process the string to replace newlines with HTML line breaks $formattedBio = nl2br($rawBio); // Pass the fully formatted string to the view return view('profile.show', [ 'user' => $user, 'bio' => $formattedBio // Pass the pre-formatted text ]); } ``` ### Step 2: Displaying in the Blade View Once the data has been correctly formatted into HTML tags on the server, displaying it becomes trivial and clean. The Blade file no longer needs to worry about string functions; it just displays the prepared HTML. ```blade {{-- resources/views/profile/show.blade.php --}}

User Profile

Biography:

{!! $bio !!} {{-- Use {!! !!} for raw HTML output, necessary here --}} ``` **Note on `!!`:** When outputting text that already contains HTML tags (like our `$bio` variable), you must use the raw echo syntax (`{!! $variable !!}`) instead of the standard echo (`{{ $variable }}`). This tells Blade *not* to escape the output, allowing the `
` tags to be rendered as actual HTML. ## Conclusion The challenge of respecting