PHP Enum for Laravel User Roles: Why and How?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today we'll have a second free video from my newest course about lateral 10 reservation project and in this case I will not shoot a particular video particular lesson but it will be kind of combination of a few lessons related to enums with roles for rows and permissions so one feature of this reservation system is the rows of guide company owner and the customer and why it may be beneficial to have enums for that on top of database relationship let me show you if you want the full course with all the 13 lessons it's available for premium members of laravel dailycom the link will be in the description below so we start this with just installing laravel Breeze and I will quickly run through that because it's very beginning of laravel 101 so I don't want to stop that but one installing Breeze by default we have registration which would fail because in the previous lesson of formalizing the database structure yesterday we decided it that we need to have role ID in users table but the default registration doesn't fill in that role so we need to see the roles for example with role create name so we add that to the database seater and here's where we add enum of role on top of the database so this will be in the database the rows each of them will have their own ID of one two three four but the question is how do you call those roles in various code parts of your application when you want to check the role when you want to get the role name somewhere or role ID would you refer them as one two three four or by name administrator company owners and others so this is what enum can help us to solve so in phpstorm we may generate just a new PHP class which would be enum this is the template of phpstorm and we Define them like this and it may seem counter-intuitive and it was counter-intuitive for me to for quite a lot of time why would you duplicate this is all in database but let me show you multiple examples how I would use that enum first example is right away after registration you need to assign role ID would you rather have number one here so imagine a future developer coming here to read this and scene number one instead or was it number two I don't even remember myself now after a minute so customer is actually a three so there would be three here it's not clear what is three actually so that's the first example right away and now if we move on to further lessons that's why I told that it's not one free lesson video it will be a combination so for managing companies for example we can search for role colon colon and in the middleware we need to check if the role is administrator so again would you rather have a number one here or readable constant of administrator greater value and this is not the only example in this file in the factory we can assign the default role of customer and have a separate state of admin with role administrator so typically in projects there will be multiple places where you would check Define or use those values of role ID let's move on to other lessons for example managing users let's check for row here and we have users which has a typo by the way I should fix that after the lesson is shot so again we get the company owner users of the company we filter by row and that role ID is a constant instead of just the number one two or three and another example is in the store method which is similar to registration that you've seen before so already how many was five six places moving on to another lesson manages users from company owner not from admin and here we have navigation example so multiple navigation bars menus layouts for administrator or company owner again instead of using one here or two here of course you could refactor it in a different way this is only one way of doing that but my point is to demonstrate in how many places what you actually use that enum in the policy the next example by the way did you know that you can override all the policy methods like this so if it's administrator allow everything otherwise we check if it's company owner everywhere and also additional conditions another example from another lesson we want to create activity and assign a guide to that activity and we get the guys from the database again not hard coding the ID of the role guide similar in edit form and also a different policy which does pretty similar thing that we've seen my point is that you can use enums on top of database IDs just to make your code readable wherever you need that IDs in the code another alternative approach it does doesn't necessarily have to be enum it could be a constant or a set of constants for example in Your Role Model maybe in your config but I guess enums is just what adds the formal structure to that and you can perform multiple operations with those enorms just read the documentation of PHP for enum classes it has more features would you agree with this approach or would you have done something differently let's discuss in the comments and again the link to the full course is in the description below and see you guys in other videos
Info
Channel: Laravel Daily
Views: 5,428
Rating: undefined out of 5
Keywords:
Id: y66BBUlVDjA
Channel Id: undefined
Length: 5min 32sec (332 seconds)
Published: Tue Jun 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.