Capitalized a blade's variable in Laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Capitalizing Blade Variables in Laravel: Mastering the Pipe Operator

I remember those early days moving from systems like Smarty to the robust ecosystem of Laravel. While Laravel offers incredible power through Eloquent and elegant Blade syntax, there are often small, useful syntactic sugar features that you simply miss until you start building complex applications. One feature I frequently wished existed was a built-in way to manipulate variable casing directly within the view layer—something like using a filter for capitalization.

You asked if you can achieve this by piping the variable: {{ $var|upper }}. The short answer is: yes, you absolutely can, but understanding how Laravel and Blade handle this is key to writing maintainable code. Let’s dive into the mechanics of using PHP functions within your Blade templates and why this approach is powerful.

Understanding Blade Filters and PHP Pipes

Blade, the templating engine for Laravel, leverages PHP's functionality directly through its pipe operator (|). This operator allows you to pass a variable to a filter, which is essentially a callable function. When you use a pipe, you are instructing the view layer to execute a specific operation on that data before rendering it.

Laravel doesn't provide every single possible string manipulation function as a dedicated Blade filter (like |upper or |capitalize), but because Blade compiles down to standard PHP execution, we can call any valid PHP function directly within the pipe.

For example, to convert a variable to uppercase, you use PHP’s built-in strtoupper() function:

php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class VariableController extends Controller
{
    public function showData(Request $request)
    {
        $variable = "hello world";
        $capitalized_variable = strtoupper($variable);

        return view('data', compact('variable', 'capitalized_variable'));
    }
}

And in your Blade file, you simply reference the variable as usual:

blade
{{ $variable }}  // Output: hello world
{{ $capitalized_variable }} // Output: HELLO WORLD

To achieve the capitalization directly inside the view without needing to pre-calculate it in the controller, you embed the PHP function call within the pipe:

blade
<h1>Original Text: {{ $variable }}</h1>

<h2>Uppercase Version: {{ $variable|strtoupper }}</h2>

This works perfectly and is a very common pattern when you need simple transformations on data just for display purposes.

Best Practices: Where to Put Your Logic?

While using filters like |strtoupper is convenient for small, ad-hoc formatting in the view layer, as a senior developer, I always advocate for keeping complex business logic out of the presentation layer.

When should you use Blade Filters?
Use them for simple, presentation-focused transformations that don't involve complex database queries or business rules—like formatting dates (|date), truncating strings, or basic casing changes like |upper. This keeps your views clean and focused on what to display, not how to calculate it.

When should you use the Controller/Model?
If capitalization is part of a core business rule, or if that data needs to be manipulated for multiple parts of your application (e.g., saving the uppercase version to the database), the logic belongs in your controller or Eloquent model. For instance, if you are working with models, leveraging the power of Eloquent ensures your data integrity is maintained. You can find fantastic examples of how to structure clean data flows when exploring Laravel's core features on the official documentation at https://laravelcompany.com.

Conclusion

So, to summarize: yes, you can dynamically capitalize a Blade variable by calling native PHP functions like strtoupper() directly within the pipe operator (|). This provides a quick and readable way to format data directly in your views. However, remember the principle of separation of concerns. Use these simple view-layer pipes for presentation flair, but reserve complex calculations and data transformations for your Eloquent models and controllers to build more scalable and maintainable Laravel applications.