Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when using Blade's echo to render enum #44431

Closed
brunoalod opened this issue Oct 3, 2022 · 2 comments · Fixed by #44445
Closed

Crash when using Blade's echo to render enum #44431

brunoalod opened this issue Oct 3, 2022 · 2 comments · Fixed by #44445

Comments

@brunoalod
Copy link
Contributor

  • Laravel Version: 9.3.8
  • PHP Version: 8.1.0

Description:

Not sure if this is a bug per se, but when using Blade's echo function to render an enum it throws an exception.
I guess this is expected, but it'd be nice to add a check for this in the echo function and either render the name if it's a non-backed enum, or the value for a backed enum.

function e($value, $doubleEncode = true)
{
if ($value instanceof DeferringDisplayableValue) {
$value = $value->resolveDisplayableValue();
}
if ($value instanceof Htmlable) {
return $value->toHtml();
}
return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8', $doubleEncode);
}
}

Steps To Reproduce:

return view('my-view', [
    'enum' => MyEnum::myValue,
]);
// view
{{ $enum }}
htmlspecialchars(): Argument #1 ($string) must be of type string, MyEnum given
@brunoalod
Copy link
Contributor Author

Maybe something like this:

if ($value instanceof \UnitEnum) {
    $value = $value->value ?? $value->name;
}

I guess another option would be to use the Htmlable interface directly in enums.

@driesvints
Copy link
Member

Heya, thanks for submitting this.

This seems like a feature request or an improvement so I'm converting this to a discussion instead. It's best to post these in the discussions section in the future to get support for your idea. After that you may send a PR to the framework. Please only use the laravel/framework issue tracker to report bugs and issues with the framework.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants