How to capitalize the first letter in Laravel Blade Views?

Solved
bitmap

Oct 17th, 2020 02:11 PM

Hi artisans!

I want to capitalize the first letter of some strings directly in my blade view.

I know that I can do this in my controller with Str::ucfirst() but do I have to use the Str class in all of my blade views as well?

Any help will be appreciated!

bobbyiliev

Oct 17th, 2020 02:13 PM

Best Answer

Hey there!

What I usually do is to use the ucfirst() PHP function directly in your Blade view.

To do that in your view directly, you can use the following syntax:

{{ ucfirst('your string here') }}

Or you could even pass in a variable if you had to:

{{ ucfirst($user->first_name) }}

For more information make sure to check out the official documentation here:

https://laravel.com/docs/8.x/helpers#method-str-upper

Hope that this helps!

Report
2
thinkverse

Oct 17th, 2020 08:12 PM

@bobbyiliev wrote a good answer. But if you want to use pure PHP you can always do that.

{{ mb_strtoupper(mb_substr($user->first_name, 0, 1)) . mb_substr($user->first_name, 1) }}

Which is really what ucfirst() does under the hood. But you can use pure PHP functions in your blade files so wanted to point this out too. 🙂

Report
2