How to capitalize first letter in Laravel Blade
Solution 1:
Use PHP's native ucfirst
function:
{{ ucfirst(trans('messages.welcome')) }}
Solution 2:
Add a blade directive to the app/Providers/AppServiceProvider's boot() function:
public function boot() {
Blade::directive('lang_u', function ($s) {
return "<?php echo ucfirst(trans($s)); ?>";
});
}
This way you can use the following in your blade files:
@lang_u('messages.welcome')
which outputs: Welcome
You're @lang_u('messages.welcome') :)
Solution 3:
Another way to make capitalize first letter using PHP and blade.
Controller
return view('stock.uk-lse', ['name' => 'djan']);
View
<h1>{{ ucfirst($name) }}</h1>
Solution 4:
I think that the best option is use CSS text-transform property
In your CSS file:
.lowercase {
text-transform: lowercase;
}
.uppercase {
text-transform: uppercase;
}
.capitalize {
text-transform: capitalize;
}
Your blade (html) file:
<p class="lowercase">{{ trans('messages.welcome') }}</p> <!-- This will display welcome -->
<p class="uppercase">{{ trans('messages.welcome') }}</p> <!-- This will display WELCOME -->
<p class="capitalize">{{ trans('messages.welcome') }}</p><!-- This will display Welcome -->
Or, the best option for me, use bootstrap
<p class="text-lowercase">{{ trans('messages.welcome') }}</p><!-- This will display welcome -->
<p class="text-uppercase">{{ trans('messages.welcome') }}</p><!-- This will display WELCOME -->
<p class="text-capitalize">{{ trans('messages.welcome') }}</p><!-- This will display Welcome -->