PHP Enum for Laravel User Roles: Why and How?

preview_player
Показать описание
A practical example of PHP Enum classes.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

In eloquent you can use enum without the value property, example User::where(‘user_id’, Role::Administrator)->get(); Looks cleaner

JoseFranciscoIT
Автор

It's magic how you read my mind for every single upload!! Just yesterday I searched "laravel role enum povilas" and here you go!

hardiklakhalani
Автор

I personally like enums-only (without DB table) style, consistency checked on application level. That makes code clear and there is one source of truth.

tisodotsk
Автор

This is nice when you have an integer as an ID, but if you're using uuids then you gotta use the name of the role and in that case access the database to get said uuid. But in that case you can just put everything inside the Role model and skip the entire Enum :)

GorimLivingstone
Автор

Small improvement idea for that. Cast model field so instead calling ->value just can do a check for enum.

pawelxyztemp
Автор

I'm using something similar with a class that in addition to providing constants for role ids, also provides a few methods like Role::list() or Role::select() to easily retrieve roles in certains ways (::list() returning an id => display_name array, ::select() returning a name => display_name array, etc)

CharlieMerland
Автор

Make sense as those ids are highly likely never to be changed. However IMO it's not really case for enums because ids are not constants. Enums are more about role names rather than ids but anyway

antonmykhailovskyi
Автор

Interesting. I've grown used to declaring public const in the model, it seems shorter and more concise (no using "->value" every time)..
And the code seems readable, since new developers know whee to look.

Are there any significant advantages of enums that I'm missing?

hrsa
Автор

Interested, any reason why you wouldn’t use a backed enum with the role name? Then use the role name as the constraint for the relationship.. (indexing the role name on roles table)

Also the seeder could loop over the enum cases to create the roles if it were backed..

Am assuming the roles have a relationship to a permission or something else? Otherwise I’d maybe just drop the roles table and handle it direct with enums..

All depends on exact use case / other project specifics though I suppose..

TheJDent
Автор

Usually I create object in the Model itself and use it whenever I need the values.

I found it more readable like this
User::ROLE['admin']

sheldonc
Автор

Does it still makes sense to create a roles table? Because you are just using the enums to indicate the role type. What is the added value of this database table? Wouldn't it be easier to just keep the role_id column as an standalone integer field in the users table?

stevensamoy
Автор

This is somewhat related but I would love to know your approach.

Imagine we have hierarchical roles i.e. we have a role MANAGER = 1 AND it must have the permissions of the roles underneath it e.g. USER = 2 etc.

Furthermore, we are expecting more roles to be added to this hierarchy as the application matures e.g. POWER_USER which will be somewhere between MANAGER and USER.

What would be the best approach to handle such roles both in the DB and the code?

ZamirMubashir
Автор

I would be agree, if after in user model role_id will be cast to this enum, and next in code just compare with one of enum state. Calling 'value' from enum for each time is like dirty solution.

mykola_antal
Автор

I prefer using enums for roles instead of having them in the dababase.

BelgranoK
Автор

Random question. Is it bad practice to have route and controller actions for "update" and "updateMultiple" records?

nemac
Автор

Thank you for the video. I think enum is good for typehinting and make some logic inside enum. If we are working with models (User our case), better to create methods like `isAdmin()`, `isCustomer()`. This example doesn't show all enum benefits.

NoNameUkr
Автор

Onlything I dislike about laravel is It totally "string" dependant. Even functions & variables pass around as string. Is there any solution to this?

hardiklakhalani
Автор

My co. Stuck on php 7.4, not moving ahead..

shubhamsahuSD
Автор

What a hell was that? Why do you use enum value everyware? Also emun has a great feature - you can compare it.

DooMer
Автор

TBH, i hate that PHP enums need you to call ->value for it to work. every other language doesnt do it.

tagKnife
join shbcf.ru