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.
@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?
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 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.
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?
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().