Part 1/3: How to Install & Create Spatie Roles and Permissions CRUD from Scratch in Laravel 10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to learn about spacy roles and permission in laravel okay so guys we are going to use a spacy package for this so now guys let's get started so first step let's go to our spacy documentation for the laral permissions so now guys let us install this package in our laral application so for that let's install here installing in laravel so just click on it and you reach there so guys this a installation command here let's copy that and open in a new terminal let me Zoom it and yep right click on the mouse and paste it hit enter so now guys let's wait until it gets installed so guys our package is completely installed and now let us minimize this and let's get back to our documentation so here we have done with the installation and now now guys this is optional part you can add this or not okay so let us just add it manually so let's go inside the config app.php inside that app dot we need to move to Providers and paste this class so let's copy this and get back go to your config app.php and move at your providers and add that so we add here that's it save it and now close it get back to your documentation and you should publish the migration and your permission so for that let's copy this and paste done hit enter okay so our files are published here as you see in our config folder we have permission. PHP file now and now guys we need to set up the here has R inside the user model so let's copy this has roll and get back to our app models user.php so in this user.php we have to add that has roles okay and please import the class so here we don't have let me directly import it which will be provided by the PHP intellisense okay so here is our imported class and now guys let's migrate this into our database all the permissions which have we got so you'll be finding one file a migration file here which you see let me just show you inside our database so go to your database migration where is create permission table okay so this is our table guys we are going to migrate this now PHP Artis migrate hit enter so migration is created and now let us see few tables have been come over here so right now you see like we don't have any permission related tables if you refresh you'll get four to six tables okay so you see this is permissions rule rule has permission and model has permissions and model has R we'll get back to our editor so now guys let us begin with creating the uh complete crowd operation for roles and permission so guys first we'll be creating with the permissions C operation so let me close everything and let's begin with new terminal here and just create a controller named permission controller so PHP Artis make colon controller permission controller now hit enter so our controller is cre created guys and now you can access that inside app HTTP controller and here you find your permission controller so first step guys let's create a function public function and the function name index open and close CBR so same way you have to create other five functions about create edit update delete so let me just create it like create and then with the edit and then again for update update and then destroy okay destroy that's it and now guys let's go to our web routes so routes web.php and move at the top let's create a resource route route colon colon resource inside that you paste that permissions and then you call your controller so let me just call my controller Here app HTTP controller back slash then permission controller colon colon class okay so we are not going to Target at one function so what we have to do we have to just remove this array and directly paste it okay so if you are using a resource so no array will come and now guys our controller is connected with the route and now let's return View and display the permission page so inside one rules and permission folder I'll keep the permission folder and index blade file so let's copy this and create the path accordingly go to Resource views create a folder name roles permission roll permission so here we are and now let's create one more folder named permission so we have permission and inside this you create a index blade file index. blade. PHP hit enter okay let's again create one more file named create blade and edit blade so create blade. PHP and copy and paste and mention edit blade. PHP so we have total three files now let's get back to the index blade and let's extend our layouts guys so if you don't know how to extend a layout please watch out the video link given in the description how to create a layout using your blade component or a normal component okay X app hyphen web hyph layout so this is my extended layout and now guys you can directly begin with the design so I will start with the container row column md12 and inser this a card card header and a card body so inside this card header I will add a H4 tag like permissions and a tag like add permission okay so let's design this as a button primary flow to end so you can create a URL here URL function and inside that please mention your route so what is the route we are creating here permissions let's get back to our index permission forward slash create okay so now let's get back and see the output for this let's move and see the permissions URL perfect so we have got this UI guys and I just wanted to give some space from Top margin top five refresh cool so now if you click here permission create it just goes to the permission create and reaches to that create method okay now you can copy this again view paste it and here change the path to create blade. PHP okay so we go to that create blade and now guys you can just copy the same whatever you have done in the index blade file let me close this terminal copy and paste inside your create blade file and create permission and this will be our back button now make it to danger and make it as B back and this will be your permission form okay so let's design this form guys in a very quick way so form action method post and the form URL and use a URL and use the same route permission and now begin with the form design margin bottom three label for the permission name and an input tag with the name attribute name and design the input box using bootstop form control class and a simple submit button so margin bottom three button save and design this button button hyphen primary and this button type will be submit so once submit button is clicked it goes with the form action method post post and in larel you required a csrf token so once submits it goes to the URL web.php so we have used a resource route so yep it goes directly to the store function so once you submit on this form URL okay so form action add this URL using post method if you submit it goes to the store function so let us create a store function here now so public function and the function name is tore and now guys once the form is submitted you are getting a request so RQ EST request dollar request First Step let's do the validation request of validate function inside array use the name attribute name of mentioned in the array bracket so we can be more clear it is a string and it's also unique U and I qu e which is going to be in the permissions table okay so let me copy the table name here so we have a permission table let's copy this and paste it give name okay and then guys once validation is done you can use the permission colon colon create method to create the data so if you don't know how to use this you can just follow this documentation let me just show you in the top like basic usage so here you use like here you create the rule permission so I'm using this format now to import this class you have a class Also let's copy this class and import at the top so paste here okay so name column and dollar request of name once permission is created you can return redirect to which page to the permission page so I want to get back to this page okay so this URL permissions and on that you can add a message status and mention the status permission created successfully so now guys let us copy this status and display in our index blade file before the card is a if condition session of status and use alert alert of success open and close CRA session of status so you can give a margin top three for this okay so let's get back to our controller and now guys let us test this whether it stores or not so first let's begin here with the permission create so now guys let's create the permission so permission is basically nothing but about setting Your Role like he can create a role edit a role update a ro delete a ro and so on it goes with the other modules also so guys let's tell create role and save so here you see permission created successfully and let me see in our table so we get here cool okay so let's get back and create more permissions like uh he can create and he can view roll let's get back update rooll and delete Ro let's save and we have four records perfect so now guys let us display this permissions inside this permission table so let's get back and here we have on this index function we are going to a index blade file let me just show you so here we are and now let me just design a simple table using bootstrap classes so table and and then and now user table head table row table heading th that is ID name and one action button for edit and delete and then guys table body table row and your data here table data so now let me just show you refresh okay so we have created the table and let's get this datas so it's a very simple way let's get back to our controller and inside this index function just create a variable called permissions equal to and your permission model so permission K get function so it will get all the permission record and this permission is a spacy model class okay and now let's copy this variable and pass inside a compact function or just pass using an array it depends on on you how you want to access okay so now we can copy this permission and user for each Loop paste your permission of permission and you can Echo those datas here of ID copy and paste then name and last Ed for the edit and delete button let me just arrange this for each Loop correctly and here a tag which is going to be edit and this is will be a delete let's use a button success for edit and create a route URL which is going to be inside web.php we are using a resource route so resource route follows one structure so I will just just show you here directly permission forward slash and then concatenate and again give forward slash and edit okay and in between you have to mention the ID so this is my ID just copy that paste here done and now you can get back refresh so we have got all these rules list and you get a edit button so let's click on this edit here you are on this URL and which is exactly targeting to your edit function in your permission controller so let me just return and show you like yes yes I am addit so if you refresh now you'll see that yes I'm edit now okay so guys let's remove this and view the file so use a view function to view that page blade file so instead of create let's use edit file what we have created already let me just show you so here we have a edit blade file okay so inside this edit blade file we are going to just copy the same file of create blade. PHP of permission okay so let's copy this and paste here and now you'll be able to see that same form has come on the edit page okay so this is my edit permission let's get back and just change the headings and few more things okay so this is my edit and now you're are going to update the record so if you're using a resource routee you have a specific route to update also so just add this permission forward slash and conatin it with the ID which is going to be permission of ID okay so from where are you getting this permission variable so we have to pass that from our edit okay so here we will mention like um in the URL you see that we are passing the dynamic ID here so with this help of dynamic ID we are able to add access in the edit function okay so use a model that is permission dollar permission okay and now let me return and show you what exactly we want so refresh so here you get that edited Ro data you are accessing let's get back and let me just show you on this update Ro so here you get update Ro data okay on this edit so guys we have the ID we have the name so all this variable only we are going to just pass in the blade file as simple now guys just create an array or a compact function and pass the permission variable done let me just remove that and now guys you are able to access this variable inside your edit blade. PHP now you can use a value and paste the variable dollar permission of name so we got the name also and change the button name to update so once you click goes to the action with the method post and Route will be submitted using a put method okay so use a at method which will be P put in a capital letter okay done and now guys if you get back refresh so here you have your data okay edit so once you click on this update it goes to the web route that is your resource route and goes to the product controller at your update function so on this update function you're getting a request like input requests so you can handle that by your request and also you are getting a permissions data like your sending the ID okay so let me just show you onor edit blade you are sending the ID also while form submit so by default it automatically understands that we are sending a complete permission record in a single object okay so you can get that permission okay so you have your permission data and also the input request so first we are going to do the form validation let me just copy this and paste here and we need to check whether is it the same record or not so for that you can use dollar permission of ID here okay so it will just ignore that if it's same record and now you can use the same data to update so here use update method and use this directly variable instead of your model okay and remove this colon colon and then it redirects back you to the same permissions URL and here mention the message as updated successfully done and now guys let us test this whether update is happening or not so refresh so here we have a permission name that is update let me update as edit Ro okay so let's click on update here and here you see that update is changed to edit R okay so we have done with the update also and now guys finally we are going to work with delete option so let's get back to our editor and yep let's go on the index plate and here is our delete so guys there are many ways to do uh this delete function like you can create a form tag and just delete or you can use this URL method also but you have to just change some logic in the web.php okay so let me just design this button first using danger and use MX2 and set up the URL so I will create a same URL as here we have in the edit let me paste it and instead of edit let's make it to delete and now guys you need to go back to your web.php and copy this paste here again and just make it to get method and here you need to change the route so as you follow here you have to mention okay and instead of permission ID here mention that I'm getting Dynamic ID Dynamic permission ID okay in the curly bra and now guys we are targeting to a single function so you have to mention that in a class and give comma and tell that function name so we are targeting to a destroy function done so now guys we are getting a permission ID with the URL let's copy that and go to your destroy function and tell that we are getting a permission ID and you have to find this permission ID so now use a permission model colon colon find function and just find that permission ID and from that find that permission ID you can delete this permission record permission equal to and just use delete so you will find that and just delete that and once deleted you can redirect back to the same page on the permission and mention here deleted successfully now guys let us test it refresh okay so let me add a dummy rooll like yeah dummy roll enter to save okay we have a new record here let's click onto delete let's click here delete so it's deleted and permission deleted successfully perfect guys so guys we have seen a complete Crow operation for our permission so now guys let's begin with the rules so we can manage our uh user management system for rules and permission so yep let's move back and let's close everything and let's be clear with the creating our controller again and now guys just change this controller to roll controller hit enter so your controller created let's move here we find our controller okay so guys to save the time I'm just going to copy the same uh permission controller code okay so let's completely copy this and we'll be modifying it according to our role requirement so let's paste it okay and yep let's go back to our documentation and here we have how we create a role so let me copy this import class of the role first we have pasted the role and now wherever you have permission just replace it everywhere okay so I'll just select here and just do control DD D and paste as a role and now let's change this variable also controll D roles index is done let's move below at the store so here we are doing the same validation role create on the same column so no changes and then with the here mention role and roll let's change this variable also here and here roll and guys again this in the update R and make it to roll so just change it and here it will be a rules table in the validation and also guys in the top store function I have missed it so mention it's a rules table okay now we can get back and update to R variable whatever you update here roll and then let's move with the delete option here in the model is rooll and here you get a ro ID not a permission ID so mention that roll ID and here also roll copy and paste okay so our variable changing is done and now let's begin with the blade files so instead of your permission let's make it to roll folder and paste here roll let's move to edit mention roll that's it and now guys let's begin again with the uh redirect function so we on the store we have a redirect function make it to roles r s and where is another redirect function here in the update and delete we have okay and now guys let's begin with the message changing part here roll created successfully and again roll updated and roll deleted done so guys our control set up and now let's create the files so let me arrange everything and go resource views we have a r permission folder so inside this Ro permission folder let's create a new folder called roles hit enter okay so let me just copy this complete three files of your permission and paste inside the rules so we can save the time okay so now guys let's begin with the index blade file okay so inside this index blade file you need to change all your title and how you display your records so first step let's begin with the title here like just mentioned roles and here add Rule and now change the route here make as rules create and then guys here we will change the variable Like rules so why are we changing this because we are forwarding the rules variable okay this is not linking correctly uh because we have the folder name as rules here okay so make it to roll hit enter so it roll now now we can just check here perfect so it's linked let's go to our roll index blade and paste it rules variable cool and here let's make it to roll and now guys let's just replace roll and here also just make it to rules okay so make sure it is as rules complete so yep and now guys you need to create a route so you can access this index function so yep let's move to routes web.php and just copy this and paste here and mention roles and go to that roll controller roll controller done and now let's try to access the rules page Ro rules Okay cool so we have got the role page guys and let's click on create here so it's going to create permission page so we need to see whether it's going to correct page or not so here we are on the create function and here a blade file of RIT linked let's go there and here you see we have not changed the title so it is telling create permission let's change it to roles and yep let's change the form action and Method post will be submitting the same and here we'll mention as a role name and it's going to be the same name attribute so once form submit is go to the action roles with the method post and which goes to a store function on this rooll controller so here we are and once it's submitted we are checking on this unique role name and creating a role so once getting back to the roles page rooll created successfully so now guys let us see whether it creates or not so refresh we are on the role create page let's create a super hyphen admin so let's click to submit so here we have saved the record and let me create one more role as admin let's save so here you see we have two roles now so now let us see with the edit and delete option Also let's move back to our edit function so this is our edit function which goes to the role of edit blade file so let's move there and change the title and other things so which goes to the rules and here once pop submits it goes to the action rules with the role ID so Ro role ID make sure you are passing this correct variable inside your edit function okay so we are passing the exactly role variable so we can access here okay so rle ID rle name change the label and once update it goes with the action okay and here it takes the route method to put and then it goes with the controller add the update function so on this update function we are doing with the RO validation and then updating the record okay so once done your rooll updated successfully so now guys let us test it again so here let me add a new um like staff okay so I have added and now let me change this to staff to user so let's make it to user update here okay so it's updated okay it's changed from staff to user okay so guys now let's begin with the delete option for this role so let's get back to our editor on the index blade. PHP off a rules folder okay so here on the r it goes to the URL like this and you need to create a route because this route is not available in the resource copy and paste and here we change the route and make this route to get method and here your accessing a dynamic ID so you can get as a curly brace here and mention rle ID okay and this goes to a particular function that is destroy okay destroy function on the roll controller so now guys you can get back to your Ro controller add destroy function and you are getting a unique ID that is your rle ID okay and you are finding that rule and deleting the rule cool and you'll get a message so now guys let's get back refresh and let me create a dummy rule hit enter so your dummy rule is created and now let me delete this so here we go it's deleted perfect so done so guys our roll CR also is done and now guys I just want want to create a button here like we can switch between the rules and permission so let me just add a simple include over here let me close this files and inside this R permission I create a file like nav links do plate. PHP hit enter and just create a simple div a tag design this button button button primary has rules permissions okay MX2 and this will be info MX2 and just create a route here like URL roles let me copy this paste here and this is going to be permissions and one more guys for the users like we are going to create a users also so let's paste Here and Now guys let's include this file in our all the blade files okay so yep let's move on the role index blade first just use at include function and nav hyph links so here we have so now guys you can copy this and paste inside your permission also permission index blade and done so now refresh here so we get this rules permission and users we don't have right now so guys let me just design them in a center I just add a class Cod called container and from margin top just give them three that's it so you can just change this button as warning okay so it has a different color okay so now guys let's begin with the main concept like adding permissions to the role so this super admin will have a role of like he can add the rules he can add the permission he can add the products categories and so on and admin also can do the same and coming to the user so user will have its own permission and if you have a staff like uh let me just add a new roll staff here so if you have a staff he cannot delete the product nor update the product nor add the product but he can only see the products only admin can add update delete but staff cannot add update delete only he can view the product so guys let's add those permissions for this rules okay so now guys let's get get started so let me create a simple button like add or edit permission for this Ro okay so yep let me close everything and let's begin with the fresh so getting back to this roles so we'll be going inside the resource views and we have a ro permission folder inside that Ro folder we have index blade okay and now so let me copy and paste and change change the button name as add or edit rule permission okay done and now guys you can create a new URL give permission permissions so now guys you can copy this and create a route and go to your routes web.php and we are working in the resource folder I mean rules part okay so you can just create a route colon col and get method and paste that route here and this is going to be a dynamic Ro ID so let us use open close CRA and mention Ro ID Here and Now guys let's target to One controller that is your Ro controller at a specific function so let me just copy and paste here so let's create a function so add permission to roll so let's copy this and go to our rooll controller so we are on the RO controller now and yep at the last let's create a function public function and the function name add permission to roll and then guys first we are getting a role ID from the URL yep and first we are going to find that role so dollar role equal to your role model use a find or fail function find or fail function and use the rule ID so if the rule is found then you're going to return and view this rule in a form so because we have to show all the permissions in the blade file and then you are going to update like that right so let's give the path guys let me just copy from the top so yep roll permission folder and inside that add permission okay now you can create a blade file let's move and create a new file called add permission blade. PHP hit enter all set and now let me just copy from the edit blade file let's copy this and paste completely here so first step I will just tell like this is my rule rule colon and your rule name exactly what you have dollar rule of the rule name so we can just copy that variable here and pass inside this view dollar R okay we have sent it now now you can access this Ro variable insert your add permission blade file and now guys here we are going to just Loop all the permissions whatever we have stored in our database so guys let's get back to our controller and at the same add permission to roll function you are passing all the permissions also so let's tell permissions equal to your permission model colon colon just get all okay or use get method to get all the data and guys make sure you're importing This Cloud so here you select with the model part okay not contracts so I selected with the model and now you are passing this variable permission permission and paste here permission variable and now let's go back to our add permission blade. PHP and now guys we have to change this action which we have already created like this let me just copy this same URL and paste here okay and you need to change this R ID to your Dynamic ID so let's concatenate here dollar roll of ID okay R of ID and then you're going with the put method itself and now let's Loop all the permissions whatever we have sent from our controller so just change this label and now let me just design a the UI here so this is my Ru and column inside this column I will add my input and this input name will be like permission of array okay so I have multiple permissions here because I'm going to Loop that so let's use for each Loop and get the variable so we have passed this variable right let me just make some spaces so it will be more clear now let's paste here so we get this permission let's add permission and now let me just control X and paste here column md3 will be looping so guys this is going to be a checkbox and let me keep in a label here open and close inside that label let's paste the input let me break it down so we can understand it clearly okay so this is my value and this is the form control and this is my input end here okay so copy this permission of ID here in the value and then at the last after the end of input dollar permission of the permission name okay and now guys let us see the output first so yep let's move Let me refresh okay so add or edit roll permission let's click on it so here you see that we have create Ro view roll okay so this is a checkbox but it's not looking correctly why because we have a design here let me remove that and now it looks fine okay so here you see cool so guys uh let me just make this more smaller like colum md2 and will be more clear cool so guys we have listed all the permissions so now let us write the query to update this get back and so once you click on update it goes to the form action with the same URL let me copy this and it goes with the method put so so let's go to our web.php and let's paste it so here we find the same URL right so I'm going to remove copy and paste here you're going to change the method route method to put and it's going to the same URL and let's tell here instead of add permission let's make it to update permission or give permission to a r okay so let's copy this give permission to a rle and we'll create that as a function here so public function the function name open and close CRA and now guys First Step you are accessing the RO ID so you have a role ID on this form submit you are getting the role ID and all the permissions you are getting from this input request so let's copy this get back to your controller here let's access the request dollar request and your Ro ID and now do do the validation so request of validate function and here you mention that I need permissions from this input okay so you can copy this just mention it is required and there are many validation guys like about array validations and all you can just find out and do that so we are concentrating on rules and permission part so now let's find the rule equal to your rule colon col find or fail function so first I will check my rule ID is correct or not so if the rule ID is correct then you will sync the permissions for this role okay so let's get back to our documentation and in this basic usage let's come below and you'll find that yeah sync permission to a role or assign permission to a role there are many ways to assign it okay so let's copy this and paste here let me remove this so guys instead of this variable permissions so you have to tell like request of your permissions permissions okay so what is this permission it's nothing but your input form that is your checkbox so we are taking that all and just syncing that so once sync you can return and redirect back return redirect back retired back with the message like uh permissions added to roll okay to roll done and oops I have missed here like mention status here okay so you can copy this when you submit this form it redirects you back to this form itself okay so it doesn't take you anywhere else so you can just show that message here um get back on this add permission at the top of the card just use if condition in a session session function of status copy this and alert alert success and open and close and paste your session of message and guys coming back to your validation also so you can just use this just add that at error okay and mention that in a single code and you can add that as span text danger and dollar me a message that's it so this will be your validation for the input request of your permission done so now guys let's get back let's submit the uh uh permission so for this super admin he can create the rule view the rule edit Rule and delete role so let me just show you inside my database so rule has permission okay so let me just show you here you don't have any record right now so once you submit let me submit okay so there is no permission named one on guard web so here is our sync problem so guys for that let's get back to our add permission here and here in the value we have mentioned the ID so instead of ID you have to mention name so now guys let's save it and get back let me refresh and now guys let's add this permissions to the super admin role let's click on update perfect so permission added to rule okay so let me just show you inside my rule has permission table cool done so yep guys now let's get back and once it is added guys it should be also checked here right so here you see that it is not checked but we have added it so let us write that condition Also let's get back to our add permission View and inside this input box only we have to just write some code so let me open and close CBR and mention uh in Array a r r a y array function and here you have to pass pass your the permission name or ID and also all the permissions we required okay so we need to create one more let me just comment right now and get back to your rule controller on this view add permission to roll blade view inside this you have to get all the records so roll permission so we have all the rle permissions and here here you can use the database query directly or you can utilize your permission model so I will mention in this DB of table so let's import the DB class here okay so I'll just copy this and use add the top so DV and let me move top and paste here just add use at the beginning and now get back so my table name that has permissions so this is our table like rule has permission here so copy this get back paste Here and Now guys let's use a where condition and you need to check Ru has permission table rule uncore ID column comma dollar rule ID rule ID so here you have that you have find the rule and from that rule only I'll get it and let me break it so it will be clear to understand and now guys let's plug the role has permission do permission ID so which means this column name okay so permission ID and R ID we have two let me just copy that column name permission ID and you can paste here so now guys same thing we are going to copy and paste again here so this is creating like a key and value like associ type and now whatever it is just get all R permission and now let's pass this and send it so now guys let's copy this roll permissions and get back to your add permission blade file and in this array let's use that okay and then let's put a condition like if it is found then make it to checked else keep it null now let's get back refresh and you see that all the checkbox are checked here so let me just add few more permissions let's go on the permissions and add like you can create permission let me make it this way save okay make it back view permission so edit or update permission anything you can set so I follow like updating okay so you can update the permission and then get back and just change this to delete permission save it cool so guys you see that our permissions are created let let's go back to roles and on the super admin we are add or edit roll permission so here you see we have a super admin and these permissions are not given so if you want to give you can just give him okay now if you update so all the permissions are given and here it's checked also cool so guys we have done like adding permissions to a rule
Info
Channel: Funda Of Web IT
Views: 4,265
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: K5493ld9n60
Channel Id: undefined
Length: 52min 21sec (3141 seconds)
Published: Sat Feb 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.