MasterPHP.in
Try Compiler →
Laravel Tutorial

Laravel Blade Basics and Displaying Data


Until now, the invoice page in your app was just a few lines of HTML with a value or two inside. The owner now wants a proper invoice page that shows the company name, the client, today’s date and the amounts in rupees. There is also a worry. Some clients type strange things into their names, and whatever they type will appear on that page.

In plain PHP you would mix HTML with <?php echo ... ?> tags, and you would have to protect every single value by hand. One forgotten call can let a visitor run their own JavaScript on your page.

Blade is Laravel’s answer to this. You write normal HTML, add a few short tags, and Blade takes care of safe output. In this lesson you will learn what Blade is, how to display data with {{ }}, why the output is safe, and how to format rupees and dates.

 

The Problem with Plain PHP in HTML

Here is how an invoice line looks when a page is written with plain PHP, the way many older projects do it:

<p>Client: <?php echo htmlspecialchars($invoice['client'], ENT_QUOTES, 'UTF-8'); ?></p>
<p>Total: ₹ <?php echo number_format($invoice['total']); ?></p>

This works, but it has two problems. The code is long and the page fills up with PHP tags, so it is hard to read. And the safety call, htmlspecialchars(), must be written every single time. Miss it once, and that one value becomes a hole in your site.

Here is the first line in Blade:

<p>Client: {{ $invoice['client'] }}</p>

It is shorter, easier to read, and safe by default.

 

What Is Blade?

Blade is the template engine that comes with Laravel. A template engine lets you write an HTML page with small placeholders for data. Blade files end with .blade.php and live in resources/views. You saw this in the Responses lesson, where you returned them with view(). In the MVC Architecture lesson you also learned that the view has one job, which is to display data. Blade is the tool for that job.

Blade does not take PHP away from you. You can still write plain PHP inside a Blade file, but you will rarely need to. Blade adds a short syntax on top: {{ }} for printing values, and directives that start with @, like the @if you used in the Redirects lesson. You will study directives in the next lesson.

 

How Blade Works Behind the Scenes

A browser cannot read Blade, so Laravel converts each Blade file into plain PHP before it runs. Here is what happens to a printed value:

{{ $invoice['client'] }}

becomes

<?php echo e($invoice['client']); ?>

The e() function is Laravel’s safe-output helper. The converted file is saved in storage/framework/views, and Laravel reuses it on later visits:

show.blade.php  -->  Blade compiler  -->  plain PHP file  -->  HTML for the browser
                     (first visit)        (storage/framework/views)

Because of this, Blade adds almost no slowdown. When you edit a Blade file, Laravel notices the change and converts it again by itself. If you ever want to remove all converted files, run php artisan view:clear. Caching views on a live server is covered in the Config, Route & View Caching lesson.

 

Displaying Data with {{ }}

The double curly braces are called an echo statement. Whatever is inside is evaluated as PHP, and the result is printed into the page.

 

Printing Variables

Send the data from your controller, as you learned in the Responses lesson. Here is the show() method of InvoiceController with one extra value:

public function show(int $invoice)
{
    $data = $this->invoiceService->find($invoice) ?? abort(404);

    return view('invoices.show', [
        'invoice' => $data,
        'company' => 'MasterPHP Traders',
    ]);
}

Each array key becomes a variable in the view. In resources/views/invoices/show.blade.php, print them like this:

<h1>{{ $company }}</h1>
<h2>Invoice {{ $invoice['invoice'] }}</h2>
<p>Client: {{ $invoice['client'] }}</p>

Visit /invoices/1001 and Laravel replaces each placeholder with the real value.

 

Arrays and Objects

How you reach a value depends on what the variable holds. For an array, use square brackets. For an object, use the arrow:

{{ $invoice['client'] }}    {{-- array --}}
{{ $client->name }}         {{-- object --}}

Your service returns arrays right now, so you will use brackets. When you reach the Eloquent chapters, rows from the database arrive as objects called models, and you will write things like {{ $invoice->total }}. The {{ }} syntax stays the same. Only the way of reaching the value changes.

 

Printing Expressions

You are not limited to variables. Anything that PHP can evaluate works inside the braces, including function calls and simple calculations:

{{ strtoupper($invoice['client']) }}
{{ $invoice['amount'] + $invoice['gst'] }}
{{ now()->format('d M Y') }}

Keep these expressions short and about display only. A calculation that matters to the business, like working out GST on an invoice, belongs in your service class, as you learned in the lesson on thin controllers. The view should only show the result.

 

Default Values for Missing Data

Sometimes a value may not exist. An invoice may have no note, for example. Reading a missing key stops the page with an error, so give it a fallback with the ?? operator:

<p>Note: {{ $invoice['note'] ?? 'No note added' }}</p>

If note is missing or null, the text after ?? is printed. This is plain PHP, and it works the same way inside Blade.

 

Escaping: Why {{ }} Keeps You Safe

The most important thing about {{ }} is that it escapes the value before printing. Escaping means turning special HTML characters, like < and >, into harmless text codes. Blade does this by sending every value through PHP’s htmlspecialchars() function, which is what the e() helper calls.

 

The Danger of Unescaped Output

Imagine that a visitor registers with this as their client name:

<script>alert('Hacked')</script>

If your page prints the name without escaping, the browser reads it as real code and runs it:

<p>Client: <?php echo $invoice['client']; ?></p>

Every person who opens that invoice sees the popup. In a real attack, the script would not show a popup. It would steal login cookies or send the visitor to a fake page. This attack is called Cross-Site Scripting, or XSS.

With Blade’s {{ }}, the same name is printed as plain text. This is what the browser receives:

<p>Client: &lt;script&gt;alert(&#039;Hacked&#039;)&lt;/script&gt;</p>

The user sees the odd text on the page, but nothing runs. Quotes are also escaped, so {{ }} is safe inside HTML attributes too:

<input type="text" value="{{ $invoice['client'] }}">

 

Unescaped Output with {!! !!}

Sometimes you really do want HTML to be printed as HTML. For that, Blade has a second syntax with an exclamation mark:

{!! '<strong>PAID</strong>' !!}

This prints the bold word and not the tags. Use it only for HTML that you wrote yourself, or that you have cleaned. Never use it for anything a visitor can type, such as a client name, a note or a comment. The rule is simple. Use {{ }} by default, and treat every {!! !!} as something that needs a reason. You will return to this in the Laravel Security Best Practices lesson.

 

Formatting Rupees and Dates

The value 14750 on its own does not look like money. In the Responses lesson you used number_format(), which groups digits in thousands:

{{ number_format(147500) }}     {{-- 147,500 --}}

That is the international style. Indian readers expect lakhs, as in 1,47,500. Laravel has a Number helper that understands this. Import it at the top of the view with the @use directive, then call Number::currency():

@use('Illuminate\Support\Number')

{{ Number::currency(147500, in: 'INR', locale: 'en_IN', precision: 0) }}

This prints ₹1,47,500. The helper needs the PHP intl extension, which most PHP installations already include. If you see an error about it, enable extension=intl in your php.ini file. Until then, number_format() still works.

For dates, the now() helper returns a Carbon date object, which has a format() method. Dates that come from the database in later chapters work in the same way:

{{ now()->format('d M Y') }}           {{-- 30 Oct 2026 --}}
{{ now()->format('d/m/Y') }}           {{-- 30/10/2026 --}}
{{ now()->subDays(3)->diffForHumans() }}   {{-- 3 days ago --}}

When you format the same value in many places, you will not want to repeat this code in every view. In the Eloquent chapter on Accessors, Mutators & Casts, you will learn how to format a value once and reuse it everywhere.

 

Blade Comments

Sometimes you want to leave a note in the template for other developers. An HTML comment works, but it is sent to the browser, and anyone can read it with View Source. A Blade comment is removed before the page is sent:

<!-- Visible to everyone in View Source -->

{{-- Only visible in this file --}}

Use Blade comments for notes about your code, such as a reminder about why a section exists.

 

Blade and JavaScript Curly Braces

Many JavaScript frameworks, such as Vue, also use {{ }} to print values in the browser. Blade would try to handle those braces first and fail. There are two ways to tell Blade to leave them alone.

 

Escaping Curly Braces with @

Put an @ in front of the braces, and Blade removes the @ and leaves the rest untouched:

<p>Hello, @{{ name }}</p>

The browser receives {{ name }}, and your JavaScript framework handles it. If a whole block contains many such braces, wrap it in @verbatim instead:

@verbatim
    <div>
        Hello, {{ name }}
        Total: {{ total }}
    </div>
@endverbatim

Alpine.js, which you will use in this course, writes its values in attributes like x-text, so it rarely needs this. You will meet it in the Tailwind CSS & Alpine.js with Vite lesson. The same @ trick also lets you print a directive name as plain text, for example @@if.

 

Passing PHP Data to JavaScript

Sometimes your script needs a PHP value, for example an invoice. Do not build the JSON by hand. Use Js::from(), which turns the value into valid JavaScript and escapes it safely:

<script>
    const invoice = {{ Js::from($invoice) }};
    console.log(invoice.client);
</script>

If your project does not have the Js shortcut available, write the full name, Illuminate\Support\Js::from($invoice).

 

Putting It Together: The Invoice Page

Here is the complete view for the invoice page, using what you learned in this lesson:

@use('Illuminate\Support\Number')

{{-- Invoice details page --}}
<h1>{{ $company }}</h1>

<h2>Invoice {{ $invoice['invoice'] }}</h2>
<p>Client: {{ $invoice['client'] }}</p>
<p>Date: {{ now()->format('d M Y') }}</p>
<p>Amount: {{ Number::currency($invoice['amount'], in: 'INR', locale: 'en_IN', precision: 0) }}</p>
<p>GST (18%): {{ Number::currency($invoice['gst'], in: 'INR', locale: 'en_IN', precision: 0) }}</p>
<p>Total: {{ Number::currency($invoice['total'], in: 'INR', locale: 'en_IN', precision: 0) }}</p>
<p>Note: {{ $invoice['note'] ?? 'No note added' }}</p>

Open http://localhost:8000/invoices/1001 and the browser shows:

MasterPHP Traders
Invoice 1001
Client: Sharma Traders
Date: 30 Oct 2026
Amount: ₹12,500
GST (18%): ₹2,250
Total: ₹14,750
Note: No note added

This page has no <html> or <head> tags yet, but the browser still displays it. Giving every page a shared structure is the job of layouts, which you will learn soon.

 

Common Mistakes

 

Mistake 1: Using {!! !!} with User Input

{!! $invoice['client'] !!}

No error appears, and the page looks fine until someone enters a script as their name. Use {!! !!} only for HTML you control. For everything else, use {{ }}.

 

Mistake 2: Reading a Key That Does Not Exist

{{ $invoice['note'] }}

If the invoice has no note key, the page stops with:

ErrorException: Undefined array key "note"

Add a default value with ??, as shown earlier, or make sure the controller always sends the key.

 

Mistake 3: Printing an Array

{{ $invoice }}

A single value can be printed, but a whole array cannot. Laravel stops with:

TypeError: htmlspecialchars(): Argument #1 ($string) must be of type string, array given

Print the individual keys you need, such as {{ $invoice['client'] }}. If you need the whole array in JavaScript, use Js::from($invoice).

 

Mistake 4: Forgetting the @ for JavaScript Braces

<p>Hello, {{ name }}</p>

Blade treats name as a PHP constant, which does not exist:

Error: Undefined constant "name"

Write @{{ name }} or wrap the block in @verbatim.

 

Mistake 5: Expecting True and False to Print as Words

Paid: {{ $isPaid }}

A true value prints as 1, and false prints as nothing at all, so the page shows Paid: . Blade does not turn booleans into words. Decide what to show with an @if, which you will learn in the next lesson.

 

Mistake 6: Putting Business Logic in the View

{{ \App\Models\Invoice::where('status', 'paid')->count() }}

It may work, but the view now talks to the database, and the same query might run again and again by accident. As you learned in the MVC Architecture lesson, the controller and service prepare the data, and the view only displays it. Calculate in the service, pass the result, and print it with {{ }}.

 

Quick Summary

  • Blade is Laravel’s template engine. Its files end with .blade.php and live in resources/views.
  • Blade turns each file into plain PHP and caches it, so it adds almost no slowdown. Use php artisan view:clear to remove the cache.
  • Print values with {{ $variable }}. Use ['key'] for arrays and ->property for objects.
  • Any short PHP expression works inside the braces, but keep real calculations in the service.
  • Use ?? to give a default value for data that may be missing.
  • {{ }} escapes the output, which protects your page from XSS attacks.
  • {!! !!} prints raw HTML. Use it only for content you trust, and never for user input.
  • Use Number::currency() with the en_IN locale for Indian rupee formatting, and now()->format() for dates.
  • {{-- comment --}} stays in the file, while HTML comments are sent to the browser.
  • Use @{{ }} or @verbatim when a JavaScript framework needs the curly braces, and Js::from() to pass PHP data to a script.

 

What’s Next

You can now print values safely, format money and dates, and keep your pages clean. But a real invoice page needs decisions and repetition. A paid invoice should show a green badge, an unpaid one should show a warning, and the list page must show every invoice, one row after another. In the next lesson, Blade Control Structures, you will learn @if, @foreach and @forelse to do exactly that.

Frequently Asked Questions

The {{ }} syntax escapes the value, so any HTML in it is shown as plain text. The {!! !!} syntax prints the value exactly as it is, so HTML is treated as real HTML. Use {{ }} by default, and use {!! !!} only for content you wrote yourself or have cleaned.
Yes. A Blade file is compiled into a normal PHP file, so PHP tags still work. In practice you rarely need them, because Blade syntax is shorter and safer. Keep complex logic in your controllers and service classes.
Compiled views are saved in the storage/framework/views folder, with long hashed file names. Laravel creates them the first time a view is used and recompiles them when you change the Blade file. You can delete them all with the php artisan view:clear command.
Use the Number::currency method with the INR currency and the en_IN locale, which prints amounts like ₹1,47,500. It needs the PHP intl extension. If that extension is not available, number_format works, but it groups digits in the international style.
Blade escaped your value because you printed it with {{ }}. This is the safe default, and it turns HTML tags into visible text. If the HTML is trusted and you want it displayed, use {!! !!}, but never do this with text typed by a visitor.

Share this tutorial