29

I'm trying to convert a string to lowercase in a views page (index.blade.php)

The following is what I would like to achieve.

<img src="images/teamnamesml.jpg logo">

This is my attempt

<img src="images/{{ Str::lower($matchup->visitorTeam) }}sml.jpg">

I get this error

FatalErrorException in ed1bb29e73e623d0f837c841ed066275 line 71:
Class 'Str' not found

Do I have to import the class Illuminate\Support\Str to a specific file?

3
  • Str should be in the global namespace. Did you try \Str::lower() with the leading slash? Commented Oct 5, 2015 at 20:57
  • 1
    Also, check here for a list of useful Helper Functions: laravel.com/docs/5.1/helpers (note: There isn't one for lowercase, but I think that's because strtolower exists already) Commented Oct 5, 2015 at 20:58
  • Tried \Str::lower() does not work Commented Oct 5, 2015 at 21:01

3 Answers 3

48

Why not just use the PHP built-in strtolower?

<img src="images/{{ strtolower($matchup->visitorTeam) }}sml.jpg">

Or, if you need full UTF-8 support you can use mb_strtolower($string, 'UTF-8') which allows umlauts and other fun UTF-8 stuff. This is what Laravel's Str::lower() function does.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes I can do that. That slipped my mind but now that I have asked the question it would be nice to find a solution using Laravel's Str::lower($value)
@user2759965 Just curious what's wrong with strtolower? It's a native function, it's faster and it's naming is very clear.
There is nothing wrong with strtolower, I am using it right now. However, I did ask for a solution for Laravel Str::lower() so I don't know if it's appropriate to accept strtolower as the answer.
Str::lower() = mb_strtolower($value, 'UTF-8')
25

Because in the comments you asked still, how it works in the Laravel way, so here an alternative solution next to strtolower and mb_strtolower, which also work fine.

You have to add the namsepace in front of the method, that PHP and Laravel can find the method.

So, if you want to use it in Blade, do the following:

<img src="images/{{ Illuminate\Support\Str::lower($matchup->visitorTeam) }}sml.jpg">

If you want to use it in a Controller or Model, you have to add the namespace, where Str is in on the top:

use Illuminate\Support\Str;

After that, you can call it without the namespace prefix:

Str::lower($test);

Comments

2

Consider using mb_strtolower to be able to convert any character that has 'alphabetic' property, such as Č, Ć etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.