converts to string when it is cast to string
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
Is there any way to return null if the input data is null?
@elvisdosreis I am not sure which version you are using.
But if you use Laravel 9 or above, you can do this:
str("test")->trim()->toString()
It should return you as a string.
returns an empty string I need to return null, because the database accepts null and not an empty string
@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;
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
@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;
@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();
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
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();
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
@elvisdosreis You will have to create any macros you want to use in all the projects where you want to use them, yes.
thanks
Or you could PR this method to the framework to see if they will accept it.
@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.