A possibility to opt-in to enum-specific methods that allows for string-like interface #2795
sheriffderek
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First time user here and it's also my first time using Enums. The value made itself clear pretty quickly. But it sure seems verbose.
Just the string:
return $user->hasPermissionTo('delete posts');
With the Enum (assuming it has been created)
return $user->hasPermissionTo(PermissionsEnum::DELETE_POSTS->value);
So, I was exploring ways to maybe wrap it in something.
return $user->hasPermissionTo( permission('delete posts') );
which would map back to the Enum behind the scenes and give us the best of both worlds?
Then I was thinking maybe we can just override it
return $user->hasPermissionTo('delete posts');
You could put this in the User model:
That seemed like it could work out.
But then I realized how many functions there were besides this one. https://spatie.be/docs/laravel-permission/v6/basic-usage/enums#content-package-methods-supporting-backedenums
So, that just got me wondering if there could be a way to opt-in to a "always enums" version of these functions in the library itself with some config flag -- for teams who are going to go all-in on Enums regarding roles and permissions.
I know people always love a "I've been using X and Y for 40 minutes and I have an idea!" type issues .. haha. But I just wanted to document the thought here in case it's something worth discussing. : )
Where do you draw the line! route() and view() and all those other things don't use Enums. Is it worth it? If so, then are people using them in every place!?
Beta Was this translation helpful? Give feedback.
All reactions