Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thebigk's avatar
Level 13

How to deal with deprecated str_* functions in blade templates?

I've recently upgraded to Laravel 5.8 and found out that the str_* helpers have been deprecated. Until now, I was super comfortable using these helpers in my blade templates. For example -

{{str_plural('Like', $project->likes_count)}}

...would quickly give me Like and Likes. What's the new way to achieve this with Str:: ? I'm not sure if name-spacing is supported in the template.

Thank you in advance for your time and support.

0 likes
11 replies
Sabonzy's avatar

you can use this

{{Illuminate\Support\Str::plural('Like', $project->likes_count)}}

thebigk's avatar
Level 13

Yeah, but it looks ugly. Is this the official new way of doing things?

tykus's avatar

Add Str to the aliases array in the app config file (if it is not already there):

'aliases' => [
    // 
    'Str' => Illuminate\Support\Str::class,
    // 
]

This will alias (or import) the Str class to the global namespace, so it will be available in the views as:

{{ Str::plural('Like', $project->likes_count) }}
3 likes
Sabonzy's avatar

yes in 5.8 the str_* has been deprecated read about it on the website.

if you still wants the old way u can create your own helpers

1 like
thebigk's avatar
Level 13

@tykus - Yes, I think that's one way. Other way would be to import laravel/helpers and continue doing the same. I however want to know if it's a good practice to build everything right in the controller and pass the dumb data to the views for display?

Is that a better approach?

tykus's avatar

if it's a good practice to build everything right in the controller and pass the dumb data to the views for display

It depends.

In the current case, I would have the like vs. likes calculated on the view wherever it will be displayed.

The choice between using the laravel/helpers package and going with the facades is a personal/project preference. The complaint about the array and string helpers concerns polluting the global namespace with (sometimes) thin wrappers around native PHP functions; maybe you are happy with this, while for others it is unacceptable.

thebigk's avatar
Level 13

@tykus - I didn't even know if it was a problem! :-D. Anyway, I'll go with the approach you suggested. Thank you.

martinbean's avatar

@thebigk The short answer is, if you want to continue using the str_* helpers then you’ll need to import the laravel/helpers package. Otherwise, yes, you’re going to need to update all instances to the Illuminate\Support\Str equivalent.

thebigk's avatar
Level 13

@tykus - I'm not why but adding the approach you suggested isn't working for me.

I've added the following to my Config/app.php file -

'aliases' => [

'Str'          => Illuminate\Support\Str::class,
]

In my controller I've the following -

$slug = Str::slug($string);

But I get error that Class 'App\Http\Controllers\Post\Str not found. What could be wrong? Do I need to add namespace explicitly even after defining the alias?

tykus's avatar

Whenever you are in a Blade template, you are in the global namespace, so you can use Str::slug() without the fully qualified class name. However, elsewhere in the namespaced areas of your app, you cannot expect to access global namespace will need to alias/import the Str class using use Illuminate\Support\Str; at the top of the file, or get it from the global namespoace using \Str::slug().

Hope this makes sense?

4 likes

Please or to participate in this conversation.