Part 2/3 - Roles & permission in Laravel 10 | Create User CRUD & assign multiple Roles to each user

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back and this is the part two of laral 10 rules and permission tutorial so guys as we have seen in previous video we have created the complete roles crud operation and also the permission crud operation and also about add or edit the permission for that particular rule okay so now guys let's get started with creating the user crowd operation and assign the multiple rules for that particular users okay so now guys let's get started so let's move to this uh route called users and create this route so yep let's go to our web.php first and just create a resource route so yep route colon colon resource function insert that just paste that URL called user and now you have to call your controller so what will be the controller let's create One controller guys first let me comment this or else it will throw an error so let's create a controller called user controller so user controller hit enter our user controller is created now you can access that let me align everything and let me close all the unwanted files now okay so we are on the web.php now go to apps HTTP controller where you'll find your user controller let me copy the path and paste inside the route so guys let's paste Here app HTTP controller user controller colon colon class that's it and now it's connected with the resource route yep now you can go to your user controller and guys let us create a public function with the function name as index open and close let's return and view the blade file so which goes to the rooll hyphen permission so and create a user folder inside that call a index blade file so let's move to Resource views and inside that views you have a rule permission folder inside that rule permission folder create a user folder okay and now let's create a index. blade. PHP file perfect so our file is created and now you'll be able to access the route cool so we are on the blade file guys and now let me just extend and add the format of layout so you can go any of your files and just include that okay so let's extend our layout paste here and yep let me just copy everything and paste it okay so first step guys we are just going to change the title like user uh users and here user change the url to users forward slash create and then let us just change this username and display email also and then action so here change the variable to users and what is our variable here we have not passed it let's pass that users variable equal to user model colon colon get function so so you get the data and now pass this variable in the array format here pass users of dollar user and now guys you can copy and access the users variable okay now let us Loop that users as user and get the user ID usern name and we need one more column that is user email so we get that user email and let me remove unwanted URL L here so we have a edit and delete button so we can update the URL I mean IDs here and change the url to users okay let's copy this paste here also perfect done and now let us see the output now refresh so you see okay our model is not imported let's go so here I import the class user okay let's get back refresh cool so our user view is designed guys and now we can get started with add user click here and it reaches to the URL like user for SLC create so guys this is error like that create function does not exist because we are using a resource route so it's giving that okay so now let's get back and go to your web routes so this is our user which is resource route and inside this user controller you have to to create your create method or function so guys public function the function name create open and close C let me just zoom it so let's return View and go to the folder that is Rule permission use a folder and inside that let's go to create blade file so guys let's go to our yep let me arrange it let's go to Resource views inside that we have R permission folder and inside that user and let's create a file create blade. PHP file okay and now guys you need to design the form like here in this user view let's get back and let me just copy from the rule part like create blade. PHP let's completely copy and paste here done and now guys we are starting from the container row and column md2 and a simple card which start and ends here and now guys let's change the title which is is going to be user create user and this is a back button where we are redirecting back to users so when the form is submitted you're going to the URL action called user with the method post and here is your csrf token and now guys let's give all the input Fields whatever required for the user creating okay so first it will be like your name so let's keep this now copy and paste paste paste okay so second is our e email and then third is our password and fourth will be our rules so all the rules you have to mention like what rules you can assign so we can do that in a select option so here I'll mention rules in Array format because we are going to select multiple so let me Design This select as a form control here I'm form control and it is multiple okay and now guys here is the option so you can list all the rules so select rle and use a for each Loop to Loop the rules of rule and use a option tag to open and close and dollar rule and then guys same thing we are going to copy and paste in the value also done and now guys and now guys once we click on this save button we go to the form action which takes you to the user route so let's go to the web.php and here is our user route so using this resource method it directly goes to the store function so let's create a function public function the function name will be store open and close and then guys you are getting a request from the form so get request of dollar request and now let's create create a user variable and call the user model colon colon create method so we can get the details of the user so guys before creating the records we need to do the validation so for that you can directly use here request of validate function and inside this directly call all the fields like name equal to inside single codes let's set the validation here it is required it is a string and Max will be 255 okay so same way guys for other fields also let me just show you what fields we have one is name then next is email and then next will be the password and finally a rule okay so you can just copy that and paste here so password and then email for the email validation let's mention email instead of string and give a max value and also you can add a unique and mention the table name users the colum field is email and accept ID is not required here now guys the password Here we are mentioning a string and Max will be can mention at least 20 and minimum we have to give eight at least okay and then guys finally we have one rules that is you can copy and mention here like rules and it is required okay now coming back to our create method here so first we are going to create a record for the user so let me add the name column field from the user model and the value so input request name copy and paste and it's going to be email so copy and paste email and then password okay so password is going to be hashed so let us make it to like hash colon make function and paste that request of password and now let's import this password hash okay so I just show you so this is my imported class for this hash and now guys we have created the user so once user is created you get the data okay so you get the data here and now let's use the um function for assigning that so you can find in your documentation so here you see that we can use the sync roll option also and let me just show you in this using permission via rules okay so assigning rules here you see like you can assign single and multiple also roles at once but we need to use the sync syncing part and let me just show you where yep here is it so all current rules will be removed from the user and replaced by a given array okay so it will just remove everything and it will update the new rule for that okay so let's copy this and get back and paste here perfect so guys our role will be synced here so our roles are little different so what we have to do we have to get all the roles request so let's remove this and just paste dollar request of roles so this is nothing but your input field which you are getting in a aray format okay so you have got that and pasted here so now guys we can just redirect back so return and redirect to the page users and with the message let's say set a status and tell user created successfully with rules okay done so guys now let us test it get back to our browser refresh once okay we have a undefined variable rules okay get back and in this create method we have to pass dollar rules equal to all the rules colon colon just use a get method to get all the rule now import the rule class import and here is your spacy permission model Rule and imported now let's copy this and pass in the blade file so this is my variable rules dollar rule okay so now let's copy this and go to our blade file and you'll be able able to access this variable named rules now you can get back refresh perfect so here you see all the details are shown guys so guys we just need to show the name and we need to pass the name only so for that we can get back and let's not touch here instead we'll work in the controller part itself so while getting the rule I want to just pluck the and pass the name and as a key and the value will be okay so we are passing as a associative array here using this plug function now let's save and get back refresh so here it's not able to get so for that let me try with all function and now refresh perfect so it's working fine guys so we can just select with the admin staff whatever you want to give yep now now let's try to save the data so before that let me just show you my uh users table here so I don't have any users right now and also the model has roles so this model has Rule table is nothing but user has rules so just understand this model as user okay so user has rules of that as simple now guys let me create a super admin so name will be super admin and super per admin gmail.com password 1 2 3 4 5 6 7 8 and select the permission for the super admin and also he will be admin okay so now let's click on Save cool so user created successfully with roll and here you see your record has been inserted so let me just show you in my browser I inan database so here user has model table has two records that is RO one and two which is super admin and admin okay so model ID is nothing but the user table ID so you can just find that here user ID let me create one more user which will be admin a normal admin okay so adminadmin gmail.com password 1 2 3 4 5 6 7 8 and just select the rule admin okay not super admin let's click to save so here our user is created successfully and we have two users let me just show you okay user ID to and now guys you can just go to your model has roles so in that you see we have one more new record that is with the RO ID 2 okay so here Ro ID 2 that means it is admin role and the user ID and now guys uh I want to show all the roles which we have added I want to show in this user table view so for that just get back and and go to your index blade at this I want to add a column called what roles you have so add this rules and let's create one TD tag and now guys give a if condition and we need to check that rules is not empty so using a empty function and here just add a not symbol and now guys what you are actually checking like user rules so dollar user off you have to get that uh function name like get R names so let me just show you in the documentation if we go in this basic usage we find there so let me just move below and come below here we have like get the name of the users rules okay so you can copy this which returns a collection that means it has an array data let's paste here okay so we got the user and now let us try to use this and loop the data so first use for each Loop for each open and close CRA paste it so we have got the user rle name as as dollar rle name let's copy this and you can just print in a badge or any design format so let me just add a label here open and close and just paste that label names and let me Design This label as a badge so b a d g b a d g hyphen primary and just use mx1 and now guys let's see the output refresh cool we have got the data but it's not visible let me just so this badge classes has been changed in bootstrap 5 so let's try with something else refresh perfect so here you see super admin and admin and this user has only admin okay and and now guys let's continue with how to edit the user data with its role okay so now guys let's get started so for that you have to create a edit function let get back to our user controller and now create a public function function and the function name edit so yep so when we are editing we are getting a ID let me just show you that so this is my Ed button guys which we are sending ID in the URL so you need to access it so let's get back to our web.php and if we are using a resource route by default it provides us okay so you don't have to worry in this you have to just mention like user dollar user that's it and now let's return and let's see the output so refresh so now guys let us click on edit and yep so we have got the user detail perfect so now guys let's get back so let's get back and we have got the user record and now let us open a blade file like we need to go to that blade file for editing the user record so let me just copy a path here and paste and now let's make it to edit blade. PHP now you can get back to your folder that is resource views R permission folder inside that Ro permission folder let's create a edit blade file inside user folder so yep we have created the edit blade file and now guys let's copy the complete user create form and paste inside the edit blade form okay and now guys let's join the title here edit user and this will be the back button and same form for the the submission but here we are going to pass the ID so guys let's concatenate forward slash here and then mention that user of ID we have to get the user variable so you can just pass this a user variable like yep user of dollar user and this dollar user we can just remove this and you can access the variable directly cool so now guys you see in this edit BL file we also have the variable called rules so you need to create that also let me just add this dollar rules so what are we doing actually here we are just copying it so let me just copy this completely and paste here done and now guys you can just pass this rules also so copy and paste and let me just paste the variable okay now you will be able to access this roles detail also and now guys once we click on this edit form we need to by default you have to select the selected rule I mean given roles so let me just show you in my example okay let's refresh this cool so we are on the edit user and here we need to fill all the user details let me just get back and go to our edit blade file user edit so we are here now and in this form let's add the value attribute and just paste dollar user of name so let's copy this complete value attribute and paste for the email also yep email and email is not always changeable so you can make it to read only and here in the password you can just skip that and now guys coming back to the rules we have to select the given rule Let me refresh okay so we have the value here and we need to select like super admin has the permission of admin and super admin so by default you have to select so for that let's get back to the user controller and now guys let's select that user rule so user roles equal to and then guys you have to get the selected user data so I have this user data here and now I need to get all the rules of it so you can handle that from this rules uh you can just find it so here you see you have rules uh with rules so you can access use this let's get back so we have rules of and use the pluck function I mean the same thing the same condition let's put it here semicolon okay so now we can pass this user rules user rules and dollar user rules copy this variable and go to your edit blade file and just use in a in Array format let me just break this down so it will be more clear okay so option breaks here and yep so inside this use a in Array function so inore array function first you have to pass your rle and then comma and dollar user Ro okay so if it is found then you will tell like selected else null so let's mention selected okay so save it and now guys you can just get back refresh cool so you see that by default it is selected admin and super admin and go back and just select for this admin user let's click here admin user and you see that rule is admin okay so now guys let's begin with updating this user data so yep let's get back to our editor and when you update this form so let me change this button name as update so update this user form with the role it goes to the action and this will be sent with the post method using a csrf token and also the route method which is going to be put so mention the method which is put okay and now guys once you submit it just goes to your uh web.php and checks in the resource route and it will go to the update function so we don't have update function let's create it public function function name update open and close cies and now guys let's get the request whatever we get from the form request of dollar request and then get the user model okay so user of dollar user now guys you can access the current user data so dollar user of update function to update the user record so let me just copy from above from the store and yep guys we have to do the validation also so let me just first copy the validation and paste here like dollar request and doing all this validation we are going to remove this email part from the validation like we are not accepting email only okay and here to update the record we have to uh copy the this Fields so let me just copy this and paste Here and Now guys we are not going to update the password also so when we require that time only we have to update so this is going to be a conditional part so guys for that I'm just going to create a variable outside like yep data equal to inside array we are pasting it and let's uh merge the data here before that I want to check do we have any uh password or not so we can just add not and make this as empty empty function if it is not empty then use this password as AR Rao and append that okay and make sure you're adding a plus symbol so plus equal to you have to mention so it will concatenate that and now guys finally whatever we get we just add here dollar data all set so guys we have done with the user update part and now we have to also update the rules for this okay so when you click what about this rle if I select uh admin and staff also so at that time you have to manage that part also okay so for that the same uh syncing permission you can just do it let me copy this and paste here so once updated you can sync the role whatever request of R you get done and now guys let's redirect back once updated so let me copy this and paste here like redirect back to the same page and tell user updated successfully with the roles okay so now let's save and get back so refresh once so here guys this admin user have a rule of admin and let me add one more rule that is staff and now let's click to update so you see uh we have some issue I think we have some validation issue so let us just get back to our editor and here just add each field like below this input at error use the input field name attribute name here and just using a span tag add a text danger and open and close CBR dollar message let's copy this paste for the email like we are not doing the email part so just ignore that we are working for the password so we can update this password also and also the roll spot so just paste this role that's it now get back and refresh and now guys let's update one again here okay the password field is required so it's not mandatory while updating so you can make it to nullable also okay so just ignore that part and now let's click to submit here you see user updated successfully and I did not select the rule let me select the staff also Now update perfect so here you see guys for this admin user you got the rule as admin and staff too okay so I can just show you here done and now guys finally about the deleting part like delete the user so let's go and yep let's begin with the delete part so you have to go to your index and here is your delete URL so let me copy this and create a route because this route doesn't provide in the resource route so you have to create it separately using a get method and in that you provide this route so guys mention the ID that is user ID and it goes to a controller which is about your user controller so let's copy this controller path and paste here and give comma in single codes and go to a destroy function so let's copy the function name destroy and create a function with the same name that is destroy function okay open and close and now guys you are sending a dynamic ID that is user ID so you can access from this as a parameter okay and now you get the user ID so let's search for that user first like user model colon find or fail function and use this dollar ID and now guys you can just use this dollar user and use a delete function to delete the user record perfect and now guys let's redirect back to the same page which is users only and update the message here user deleted successfully okay so I don't know it is going to delete with the rules also so let us see what happens now so before that uh let's go and refresh here so let me add a new user like dumy user so dummy at gmail.com password 1 2 3 4 5 6 7 8 and select the rule admin and staff and user I give three roles so yep and you may see in your user model has rules so you have total three rules for this particular user ID 3 let's delete click to delete perfect so user deleted successfully let's see in our database for the users table in the users the user record is deleted and now let's see in the model has rolls so here you see it automatically deletes it okay so why because the spacy package uh database design is written like that so it will delete that record also okay so you don't have to worry about that perfect so guys we have seen about how to uh fetch the users with the roles and add the user and assign a role to that user and also edit and update the user with its role and also delete the user data okay
Info
Channel: Funda Of Web IT
Views: 3,136
Rating: undefined out of 5
Keywords: fundaofwebit, laravel 10 spatie roles and permission, Laravel 10 with Spatie permissions, spatie laravel permission package tutorial, laravel roles and permission tutorial step by stepm, spatie roles and permission from scratch crud in laravel, laravel 10 user role permission crud, laravel 10 roles and permission crud example, roles and permissions from scratch laravel 10, roles and permissions in php
Id: Q8DaK2eTr1Y
Channel Id: undefined
Length: 31min 58sec (1918 seconds)
Published: Sun Feb 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.