Replies: 2 comments 1 reply
-
If I recall correctly, enum Letters
{
case A;
case B;
case C;
}
$letter = Letter::A;
e($letter); // throws the error Argument #1 ($string) must be of type string, Letter given
e($letter->name); // echoes the string 'A'. The // Solution 1
enum Letters implements \Illuminate\Contracts\Support\DeferringDisplayableValue
{
case A;
case B;
case C;
public function resolveDisplayableValue()
{
return $this->name;
}
}
$letter = Letter::A;
e($letter); // echoes the string 'A'. . // Solution 2
enum Letters implements \Illuminate\Contracts\Support\Htmlable
{
case A;
case B;
case C;
public function toHtml()
{
return $this->name;
}
}
$letter = Letter::A;
e($letter); // echoes the string 'A'. |
Beta Was this translation helpful? Give feedback.
-
My package currently supports what you propose https://github.com/henzeb/enumhancer/blob/main/docs/blade.md |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Since Php 8.1 has already been released and Enum types is already a thing...
I think it's best that Blade's
{{ }}
echo statements should automatically handle enum types...implementing a
__toString()
method in an enum throws aEnum may not include __toString
... so using enums in{{}}
throws aArgument #1 ($string) must be of type string, EnumType given in /vendor/laravel/framework/src/Illuminate/Support/helpers.php:118
Laravel already supports
Casting
model attributes toEnums
, it would also be much easier if{{ }}
handles$model->enum->value
automatically...Beta Was this translation helpful? Give feedback.
All reactions