Laravel Stringable to PHP String

APRIL 14, 2021

Here's how to get the raw String value of a Stringable object. Laravel's Illuminate\Support\Stringable has lots of helper functions, but sometimes you want to get the raw string value. For that, you just need to use the strval PHP built-in function on an object of the Stringable class.

// Define Strintable object
$stringable = Str::of('laravel-stringable-to-string');
get_class($stringable); // returns Illuminate\Support\Stringable
gettype($stringable); // returns object

// Get its raw String value
$string = strval($stringable);
get_class($string); // returns PHP Warning:  get_class() expects parameter 1 to be object, string given in […]
gettype($string); // returns string