DEV Community

david duymelinck
david duymelinck

Posted on

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)