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

elvisdosreis's avatar

Str::of to string

I have a question at what point does php automatically transform a variable into a string?

Str::of('test')->trim();

not work

if (Str::of('test')->trim() === 'test') {
	return true;
}

in this case I need to use a toString()

but there are some situations where it automatically converts to a string

(string)Str::of('test')->trim() or when i try to insert into database using pdo bindParam

0 likes
16 replies
Snapey's avatar

converts to string when it is cast to string

elvisdosreis's avatar

Is there any way to return null if the input data is null?

elvisdosreis's avatar

returns an empty string I need to return null, because the database accepts null and not an empty string

Snapey's avatar

@elvisdosreis this data does not come from a request? By default Laravel converts empty strings to null in a global middleware.

The heart of it is a simple ternary;

return $value === '' ? null : $value;
elvisdosreis's avatar

Yes, the middleware already does the work of transforming the field from empty to null, more so in a situation where I have a table with a foreign key where I can receive the value null or a string in lowercase, when doing the command Str::of('test')->lower()->toString() or if the input is null Str::of(null)->lower()->toString() it will change to a string

Snapey's avatar

@elvisdosreis yes, so you have to test for null first or use the snippet I showed from the middleware

$str = Str::of(null)->lower()->toString();
$str = $str === '' ? null : $str;
kokoshneta's avatar

@elvisdosreis Of course it will change to a string when you use Str::of($value) – that’s what Str::of() does: converts the parameter to a string(able)! You are explicitly telling it to turn it into a string.

If the input value may be null, and you want to keep null values (or indeed empty strings) as nulls, then test that before you turn it into a string:

if (empty($input)) {
	$value = NULL;
} else {
	$value = Str::of($input)->trim();
}

// Or as a ternary
$value = empty($input) ? NULL : Str::of($input)->trim();
elvisdosreis's avatar

I could just add a macro.

Str::of($input)->trim()->toStringNull()

Str::macro('toStringNull', function (string $str) {
            return empty($str) ? null : $str;
});

only I'm using it in a separate project in Slim, only illuminate/support is it possible for me to add the macro in this situation

Snapey's avatar

in a provider (eg AppServiceProvider) boot method, add the following;

        \Illuminate\Support\Stringable::macro('nullableString', function () {

            /** @var \Illuminate\Support\Stringable $this */
            return $this->value === '' ? null : (string) $this->value;

        });

use like

$str = '';
Str::of($str)->upper()->nullableString();
1 like
elvisdosreis's avatar

this is precisely what I thought about making a laravel project, the problem is that I have a separate project that only uses illuminate/support

kokoshneta's avatar

@elvisdosreis You will have to create any macros you want to use in all the projects where you want to use them, yes.

1 like
cwhite's avatar

Or you could PR this method to the framework to see if they will accept it.

elvisdosreis's avatar

@cwhite if you want to suggest this implementation to the laravel team, i think it can be useful for someone

Please or to participate in this conversation.