DEV Community

david duymelinck
david duymelinck

Posted on

1

Laravel Request enum method

While I was reading the Request Handling with PHP Enums in Laravel article, I thought why do we need a simpler validation?

When I had seen the example, is the validation not simpler. The controller method just takes over the default redirect behaviour that happens when you use $request->validate or $validator->validated.

Now that we established that it is not for validation, why do we need the enum method?

To check if there is a value for the input? In the definition of the tryFrom method, which is the base of the enum method code, you see it returns null when the enum case is not found.

The enum method also checks if the enum exists and if the tryFrom method is present. If the enum didn't exist and you couldn't use the method, your intellisense or a test will yell at you.

In conclusion just write $tier = SubscriptionTier::tryFrom($request->get('tier'));.

I understand Laravel focuses on an easy API. I don't like that it hides even the little things.

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay