Full Laravel Spatie Permission Tutorial | Laravel 9 tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends i had a comment in my videos if i can create a admin panel with laravel and laravel permission package from spati okay so i'm going to create that i'm going to start a new series so in this video i'm going to install the level project and install also the laravel breeze and the laravel permission package so for that i have also opened the terminal and i am inside the directory where i want to install the package the project so let's create new project laravel new and i'm going to say admin okay after this i'm going to install the laravel breeze and if you go to the laravel documentation starter kits and go to the laravel breeze installation so we need composer require laravel slash breeze also i'm going to open the database and create new database for our project so inside the databases so connect first and inside here add new and add new admin ok now cd in the admin and run this command and i'm going to zoom it this okay clear this one and we need to run the php artisan breeze installed npm install and npm run there and after that we need to install the php artisan migrator create the migration but i'm going to install the composer require spotty level dash permission first so wait for the level mix okay so that's it okay now run the composer require spotty slash laravel dash permission after that we need one sort of run this php artisan vendor publish and we are okay phprt is unoptimized clear and php artisan migrate after that around my grid and okay if i come here and refresh we have all the migrations right now is empty now if you go to the basic usage we need to add in the user row mode that we need to add as a role straight so i'm going to open this project with a vs code okay so open the open the the user model and inside here add the as roles and import that so here we have the has roles save this close and that's it now if the work with rolls middleware you can see here in the basic usage we have using a middleware so to protect roads and in the kernel php we need to add this middleware inside the road middleware so copy this one the three middlewares and open the kernel.php ins inside the http okay and paste here all the middlewares save close this one and we are ready to go now in the next video we are going to create and assign roles to the user hope you enjoy friends and see you in the next hello friends in this video i'm going to create roles and create a user as admin and attach the raw admin to this user and this is going to be with a laravel cedar we can do with a laravel tinker so we we can open the tinker and create roles and creators of the user and attach the role to the user but i think it's more useful so we can create that seeder for rows and see there for the admin and we can run the php artisan migrate after seed and also is going to create the migration and also the user with roles and with the royal admin attached or with two rows at us we can see so for that if i open the project sorry here i'm going to open with a vs code and in the database seeder we in the public function run we don't have we don't do anything right now because we have this factory which is commented i'm going to create two seeders right now so let's open the terminal and say artisan or php artisan if you want so php artisan make cedar and i'm going to say roll cedar and one for the admin admin cedar okay and in the role cedar now i'm here i'm going to create three roles and let's go to the level permission package in the basic usage here we can we have the example or we can create roles role create and the name we want we need to import this role spotty permission modles road so let's copy this one go to the sitter and paste that in now we need to import the road and import the spotty permission models roads not the contracts role the models role and i'm going to copy this and say one for the admin and one for the user the first one is going to be admin the second writer and the last one is going to be user save this and open the admin seeder here now i'm going to create the admin user so i'm going to say user and import model user up here and say create and i'm going to copy what we have in the migration for the to be more to save some time and paste that in remove this and add it here i'm going to remove the remember token this password is the hash password for the password world i'm going to add this as is and in the name now we need say a string i'm going to name it admin and for the email i'm going to say uh admin admin.com and i name it admin because we have the app in here but you can say gmail.com and what you want save this but before this i'm going to say here dollar sign user and assign and after say dollar sign user and assign the role and if you go to the using permission via roles here is how we can assign a role to the user so the design user and assign role and the name we want or we can assign two rows or as an array as minerals as we want so let's copy this one and paste it here okay save and go to the terminal and say artisan migrate fresh dirt seed and right now if i open the table or the sql eyes and see the admin rows and user we don't have anything because we need to call these two seeders inside the database seeder and come here and say dollar sign this call and say roll cedar class and also dollar assign this gold and call the admin cedar and with that now if i run again the migration suit the diverse seed and here we have we seed in the roll cedar and also the admin cedar and if i refresh now the database yeah we have three roles we have a user with name admin and admin gmail.com and in the model has roles we have assigned the role with id 1 and 2 to the user with id one which is this the first one and the rows with id one and two is admin and writer let's go to the our project admin so go and login and what we have here we have admin gmail.com and the password is password so admin gmail.com and save password here and login and we are logged in as admin okay friends also we can do this so in the admin cedar instead of saying this we can assign the role directly in here and save and if i run again and refresh we have the same thing okay if you like this like this is okay so that's it for this video friends how we can create the roles and create the admin user and give it their all assembled admin or admin writer hope you enjoy and see you in the next video rants all the best hello friends in this video i'm going to create a separate admin layout so when we navigate to the admin i'm not going to add the app layout but i'm going to create a new one and also i want to show you here is the github repository for this project so github.com laraveler slash laravel dash permission ok so let's open the terminal and i'm going to cd in the code and cd in the admin open this with with vs vs code and okay now the first thing i'm going to open the web routes and here we have the route.cat for dashboard i'm going to do the same thing but just change the name here so copy that and paste it down there and say admin and also here i'm going to say admin dot index we don't have that but i'm going to create the name is going to be admin dot index as well and the middleware now is going to be authentication and also the role admin so this is coming from kernel php here we add three middlewares roll permission and roller permission and for that we can add here the road in the name of the road save this and go to the browser and say here admin and we redirect the login page because we have this authenticated middleware let's login as admin and we are a director of the admin so we need to create the admin index which was not found so let's go and create that in the viewers i'm going to create new folder name it admin and here new file name it index.blade.php index.blade.php in here now i'm going to create a new layout to work in the admin i'm going to save this as admin so go to the app layout and save this us and change the name to be admin also we need to change in the app view apply out save this one as the admin layout and change the name of the class to be admin and also the return view layouts admin save this close close this one and in the index i'm going to do the same thing we have in the dashboard so select all and copy and paste it here save come here now and refresh and we have the same thing we have in the dashboard okay so if i want to change something now because here i am in the admin layout and so for example i don't want the header here remove that save and also we need to remove from here the slots because we don't have now the slot header save and refresh go to the admin and admin layout and the index yeah we are okay what oh here we need to say now admin not app layout so admin save and refresh and we don't have the header here okay but if we got here and i want to go to the admin because i am admin i want to have a link when we click and go to the admin and that link is going to be visible only for the admin users so let's go to the navigation inside the layouts we have this navigation here okay and here we have the navigation links this is the dashboard so this one copy that and say here roll this is the blade directive from the package so roll admin give it the name of the role and anything inside this is going to be enrolled here is going to be visible only if the role if the user has the royal admin now change the road road is going to be admin dot index and the name is going to be okay so the route is going to be uh admin index because we added the name here and with that if i refresh we can we have the name here and click ok click here go to the dashboard now if i log out and go to the admin we need we redirect the login and if i logged in as test user we have the 403 in the admin area if i go to the dashboard also we don't see the link here that's it for this video friends hope you enjoy if you like uh like the video subscribe to my channel and thank you and see you in the next one all the best hello friends in this video i'm going to change the admin layout so i'm going to add the sidebar and for that if you go to the alpine toolbox and go to the examples search for navigation and i have find this useful so click there and here we have the sidebar and i'm going to copy this one now and paste inside the admin layout so in the layout admin i'm going to pass that in save and also we need to run the npm run watch to watch for the changes we made in the css go to the dashboard i am logged in as admin go to the admin now and here we have the sidebar and also if i change the preferences for the dark mode and come here i think we have also the dark mode here so pg we have dark mode like this but we need to change so select all dark mode like this and just say dark hit save and come here and refresh and yeah we have now the dark mode here i'm going to add the username and when we'll drop down at the log out link and also in here is going to be the or mine dial so here is the button here is the navigation and we have uh click away the open is going to be false now in after this i'm going to add a div with glass flex i'm going to give it a with the full uh bg the b slate five fifty and so here is going to be the h1 i'm going to say content here okay save come here and refresh and yeah here is the content now in the drop down yeah here we have the drop down here i'm going to add the username and it's going to be the same thing we have here so let's drop down and say elf username and after that add this form so remove this link and i think we have a link inside the form yet so what i'm going to do zoom it out a little bit because and here say add this select all the classes and add it here so add class paste that in remove this one and hit save come here and refresh and yeah here we have the admin and logout so if i log out we go back go and login again as admin go to the admin and here now we have the admin layer now in the content we need to say or remove this content and say just the slot dot assign slot okay come here refresh and here we have now you are logged in and this is coming from the index admin index here we have the eulectin remove this or just say here admin content and refresh and yeah here we have the admin content okay friends that is for this video hope you enjoy and see you in the next video all the best friends hello friends now here we are in the admin area i'm going to add two links here to go to the roles page when we can display a table with all the rows we have and also the same thing for the permissions and because we need to create more roles more permissions and here is how we can create roles roll create and also the same thing for and permissions we can sync permissions to the role and the same thing for the permission synchros okay so let's uh for that we need to create first i'm going to create the controller so let's open this and artisan make controller permission controller and also the rows controller okay after that go to the web routes and i think i'm going to yeah in here now we are in the admin what i'm going to do i'm going to remove this here and say middleware admin name is going to be admin dot also i'm going to give it the prefix which is going to be admin and now i'm going to say group and create a closure here and here now we need to add the default admin and i think we have the controller admin yeah we have this index controller which return the admin index so let's say here road and get slash because we have the admin here so just admin and the controller is going to be index controller import that that's controller class and the method is index we need to give a name which is going to be just the index because we have the prefix admin so admin dot index we have the name admin here so it's going to be admin dot index okay next one i'm going to add the road and it's going to be resource now resource for the roles and here we need that the controller which is robot controller class and the same thing for the permissions so here say permissions and the controller also is going to be permission controller and make sure you import that so permission controller roll controller and index controller okay hit save now we need to go inside the resources views layouts and admin blade now here we have the links and i'm going to change this one to be rolls and the second to be permissions the tag now is going to be road and road is admin roles index okay the second one is the permissioner sorry at the road first and permissions index hit save go to the browser and refresh and here we have the roles and permission and right now we don't have that the controller but here is the links so let's go and create the controllers the methods methods for controllers sorry save this and close and open the rows controller and create the first method which is going to be index and here i'm going to return the view which is not here right now so admin admin [Music] roles dot index okay and also open the permission controller and do the same thing in here create a function name it index and return the view admin permissions dot index save now open the resources admin and here create a new folder for roles and here new file index dot blade.php also a new folder for permissions and new file here index.blade.php okay come here and select all copy so in the index inside the admin and paste it in the index of the rows here i'm going to say admin role save and here the same thing but just say admin permission save come here and refresh and now if i go to the roles we have admin role here and permission we have an error here in the permission controller we need to add a semicolon refresh and we have the admin permission okay friends that's it for this video in the next video i'm going to add the table and display all the roles we have and also all the permissions hope you enjoy please like and subscribe to my channel all the best friends hello friends welcome again in this video i'm going to display the table here to the roles the same thing the permissions and here we have right now we have only the selected rewards so if i click here we are in the permission but the highlight that is there also we can fix that and display the table with all the permissions we have also the roles for that i'm going to use this uh table from the titleindui.com this is the free version if you want more you can pay and see all the tables there so i'm going to copy this one and inside the resources now in our project views admin we have the roles here and the index and the first thing i'm going to say here i'm going to add the width full here save and go to the browser refresh in the roles yeah now we have and the same thing for the permission but in here i'm going to paste the table we copy save and refresh and yeah here we have the table and i'm going to add it here padding of two refresh and yeah we have some padding here okay [Music] now let's get all the rolls and display here in the rows we need only the name because if i open the database okay and we have the admin database here so in the roles we have only the name and the word name but we need only the name there so let's display only the name and what we can do is come here and remove this and also i don't need this role here save open now the roll sorry open the roll controller and here i'm going to say dollar sign rolls is going to be with the roll motels and get all the roles okay so make sure you import the role here and paste it here so say compact and add the roads with that now we have the roads in here and i'm going to say for each dollar sign rolls as the road and remove and leave only one place it here okay and here we need the role name so just say here dollar sign roll name right now hit save come here and refresh and yeah we have three rows here now so we need to add the button to edit and delete them so let's add another dd and here for example we need to add a link to delete anchor tag under edit and want to delete so copy and paste and say here delete save come here and refresh and yeah here we are the same thing i'm going to do for the permission we don't have any permission but let's select all copy and what why we don't have the indented so let's uh no okay i'm going to indent this thing here so save and select all copy and go to the permission and select all here and paste that in just we don't need say roles here but permission so select all that and say permission and also open the permission controller and get all the permissions so to assign permissions is going to be permissions import that permission here and get all okay paste that in so compact permissions hit save from here and refresh go to the permission and we don't have any permissions yet so what we need to do also we need to fix this and we need to add button to go and create the rows or the permissions so let's close this first close this one and i think it's better if i zoom it out okay now after this one i'm going to add another div here div with a glass flex and justify end and here at an anchor tag with a shaft to go to the arrows create butter right now i'm going to leave it empty and say create save here and refresh we are in the permissions so yeah here we have that button but we need to add some classes so bx 4 and by to vg green 700 on hover accent bbg green 500 is going to be rounded uh md and i think we're okay so if i save come here and refresh yeah and maybe add some padding here and i think we are okay so here we have create the same thing i'm going to do for the rows so copy go to the roles and come here paste that in save refresh go to the roles and here we have the role and that create maybe creator all and the same thing here create permission it's save refresh and yeah now it's more okay and that's it for this video fans in the next video we are going to create the role and also create the permission hope you enjoy and see you in the next one all the best hello friends so welcome again now i have fixed these links here so for that i have created a new component which i named it admin links okay and now the links is the same link we have with enough link just change the classes and now in the admin blade i added the admin link here okay and the shelf wrote admin roles and this active if this route is the request wrote this okay and i'm going to add in the github so you can find that now let's create the create page because if i go now we don't have also the links in the index add the road and rows create save the same thing here add the road permission create save okay now refresh and if i click here we don't have the create method in the road controller let's go and create that close this close this open the road controller and create the method here so function with name create and return return of view admin roles create okay save go to the resources rewards we have this index and save this as a create blade okay uh in here now i'm going to remove everything and just say here we need the form save this come here and refresh and yeah now we have the form also this link is going to be right here and go back to the rows so let's change this remove this justify end because by default it's going to go to the start and the route is admin roles index index also the name we need to change so i'm going to say row index page save and refresh and yeah here we are okay and i don't like this i'm going to add the text to be slate 100 okay around the npm around watch okay and yeah we have now the white okay here we need the form and for that i have this form here so you can find in this late alien css.com website and this string so let's copy this and paste it here so why what we need in the create form we need only the name okay let us paste that it's save we don't need the text area so in the text i insert here i'm going to add the button so let's say button type is going to be submit and add some classes so px 4 and py 2 is going to be bg green 500 on hover vg green 700 rounded md and that's it let's say create okay next one is the removal so this one image we don't need the image and input type text id is going to be named so let's let's say name we don't need this wire model lazy when we are not working with uh live wire also i'm going to remove this one and i think we are okay save go to the browser and refresh and here it is now we need under the name add the name and go and create that okay in the form also we don't need any type multi-part form data we are not uploading images just say method this is going to be post and action is going to be went at the road which is the admin roles store okay and also don't forget that csrf token save this and if i refresh and type something here click create we don't have that method so we need to go and create let's say here function store we need the request here and dollar sign request and here i'm going to say we'll assign a valid date and request validate the name we have only the name here and let's say it's going to be required and also minimum of three characters okay and now roll create dollar sign validated and return sorry return to the road admin roles index okay save this go back and refresh so let's say new role for example super click create we go back and here we have the super row let's uh do the same thing and also if i go and create and say create here we redirect back so we need that also the if we have any error so in here and the class is going to be text red 400 and also text small i'm going to say and let's save this so if we have an error for name come here and refresh create and yeah the name field is required so let's do the same thing for the permissions i'm going to select all here copy and go and create new file create.blade.php in the permissions and paste that in we need to change some here is going to be also in the action is going to be permissions and that's it save go now to the permission controller and create the method so permission oh sorry a function for create and here we need to return the view admin permissions create and after that create a new one for storing the permission store we need the request dollar sign request and the same things so i'm going to say the validated is going to be with the request validate and we need to validate the name and just say required for now and now permission create and create the validated and return back to return for the road permission index admin permissions index save let's go to the permissions go and create permission so let's say edit and add it like this editor post edit pause go and create that and we have the edit pause here okay friends that's it for this video and hope you enjoy please like and subscribe to this channel and see you in the next video all the best friends hello friends in this video i'm going to work with edit and delete roles and permissions right now we have these links but i'm going to fix that so first go to the resources views admin and permission index okay in the table we have the this edit and delete and i'm going to cut them add a div with a class flex and justify end and here add a div with class space x and paste that in now go to the save first and go to the permission and yeah we get in the end the edited delete button and next we need to fix the start some styles so say plus px 4 and vy2 and this is going to be with bg i'm going to say blue bg blue 500 on hover is going to be bg blue 700 and the text is going to be white and also i'm going to add around that md okay copy this and do the same thing here so add class just now here we need to change the background to be red okay save this come here and refresh and when we are here we are in the permissions but why we've lost and we need to run the npm run watch so clear this and say npm run watch to watch for the changes because you see here we don't see the colors okay now come here and refresh and yeah here it is edit and delete now for edit we need to add the road to go to the permissions edit and also we need to add the permission id here and here is going to be the delete but here i'm going to add a form so let's work with edit first and i'm going to create in here i'm going to save this as edit so save us and edit blade.php here we need to say now permission update we need to add also the dollar sign permission and we need to add the value here so i i'm going to do something like this and here i'm going to add the value which is going to be the permission name okay hit save now open the permission controller and create the edit method so in here create a function with name edit here we need the permission and dollar sign permission i'm going to return the view permissions edit and add the permission there so compact permission hit save and if i refresh we are in the permissions and if i click edit we go here but we don't see the permission name and also change this and also this one so in the edit and i think also we need to change in the create here the anchor tag is saying raw index but we need to say permission index save clauses the same thing in the edit permission index okay and why we don't have the permission name because i make a mistake and refresh and here it is now we need to create also the method for updating so create a function with name update and here we need the request dollar sign request and also the permission dollar sign permission i'm going to validate the name and say dollar sign permission update and the validated and the return to the road admin permission index okay save come here and refresh edit post updated and also we need to change the create here but if i click and we have some oh yeah in the edit we need to add also the method so method is going to be put or patch what you want and also because we are here change the button to be update save come here and refresh again say updated update and the post is updated now the same thing for the roles so go to the roles and save this as edit we need to change this to be update and add the role here dollar sign roll id now the method is going to be put here we need also to add the value and i'm going to add it here so value is going to be dollar sign roll name and here we need to change this to be update okay save go to the roles and also we need to change this one in the index so in the permissions index copy this and paste in the roles index when we have this edit and delete just change the row to b rows edit and add the role id here save refresh okay click edit oh we don't have the method now so close all them and open the road controller and here create a function for edit we need the road dollar sign roll and here return the view which is the admin roles edit and also add the role itself so roll save and if i come here and refresh we have the name here and roles edit why we have this hostname changed role name the same thing in the create yeah role name and now we need also to create the method for updating so create a function say update and we need the request dollar sign request and also the roll dollar sign rule and do the same thing validated this and say the role update dollar sign validated and return the admin index save come here and refresh now if i remove this and click update we have the validation and if i say i mean updated we update the oh we changed our old admin and now let's open the database because we we don't have the role admin we had the role that mean and we updated the road so we need to fix that do not update the our old admin only the other routes so let's open this one and connect select the admin and roles and this one we need admin save come here refresh and yeah so go back we need to display only the others robots not the admin anyway so if i go to the user and say up click update and we update the underlying okay friends please subscribe and like the video and see you in the next hello friends in this video i'm going to work with delete roles and permissions and also i want to display a banner when we create update or delete the roles and permissions and i want to hide also the admin because i don't want to update the admin because in the road we add we add the revolution admin here so if we made change this is going to break okay so first let's uh hide this admin and we can use the where not in so if you go to the level permission package and basic usage so we're not in we have this here and open the road controller now instead of roll out we can say roll where not in the name in our case is admin and only the admin right now we don't want to hide anything else and if i come here and refresh yeah we have only two next one is the show the banner when we make changes and also hide that with alpine js so i have searched in the italianui.com and here we have an example so let's copy this one and i'm going to add this in the resources views layouts admin layout i'm going to add it right here so first i'm going to check if the session has message and paste that in and now we don't need this learn more this is going to be work with alpine and hide the banner so just remove this div and here now we need to display that so session get message and the same thing here so this is for breakpoints okay now work with alpine js and i'm going to say here x data and open is going to be true by default and also this x show is going to be open and now when we click this button it's going to hide so so remove this and on click this open is going to be off and uh sorry that intently this one okay and save now open the controller so roll controller and when we create or update what we want so here with the say with a message and here i'm going to say roll updated successfully you can say what you want save this go to the browser refresh go and update this one update and yeah they're all updated successfully now if i click here we hide that okay nice now the same thing you can do for the create and so just copy this and when we store here and changes role created successfully okay now let's work with the delete and when we go to the admin roles index we have this anchor tag and here i'm going to add i'm going to add the form so let's say form with a method post and the action is going to be the road admin roles destroy we need to add the road id here and i'm going to say on submit return and confirm so confirm are you sure okay now here we need that that cs csrf token also we need the other method because the method is going to be delete and also add a button to be type submit and say delete and close the form save now in the role controller we need to create that method and i'm going to say a function the name of the function is destroy we need the role here dollar sign roll and what we need just delete that so the role delete and return we need to return back so return back and i'm going to say with the message and the message is going to be roll deleted save come here and refresh and first we need to add the class here so copy this and after that we need to remove this one we don't need to save and refresh this and while we have this up and down let's add the legs here and also this is going to be space not yeah refresh and yeah now if i delete this we have the are you sure click ok and response with where is this oh sorry friends with like this go back refresh and we deleted so if i delete also this one are you sure click okay and we delete it and they're all deleted go and create and say user we're all created successfully and we have there and now the same thing for the permission so copy this form and open the permissions index and instead of this we need to add that form just change the method the action to be permissions and the permission here okay and also we need to add this flex and fix this one to be space save go to the permissions and we have something like this but we don't have the the destroy method so in the permission controller come here and create that and i'm going to copy what we have here so copy this one and paste it here just change the row to be permission okay and here also permission delete and return back with permission deleted save come here and refresh and do the the same thing for the update and so on so copy this when we update we need to say permission updated and you say successfully or not is up to you just for to save some time i'm going to just say this one so created save come here refresh and click delete are you sure ok deleted and permission deleted go and create one so i'm going to say update posts create and permission created and here we have okay friends hope you enjoy and see you in the next one all the best hello friends so welcome in this video i'm going to work with uh roles so in the edit form down there i want to display all the permissions this role has and also the form to assign a new permission to this row so let's open the vs code and i'm going to open the edit plate in the roles inside the road we have this edit and we have the four now down there i want to display all the permissions this role has and if you go to the laravel permission package in the basic usage you have here using permissions vroads and down there we have a sign in permissions to also roll and give give permission to do something if we if the role has permission to do something and also revoke a permission to do that after this form we have this thief and i'm going to add after this div i'm going to add another div or not here not here down there i'm going to not add another div and say at the class with margin top to be six and padding two and first theorem with other h2 and say class the b text to excel okay next found to be semi-board and say permission for role permissions now in here we need to add a div and another div for the four okay here is going to be the form and here is going to be the uh to display all the permissions so first i'm going to check if the road has a permission so i reload permissions and down there make a forage dollar sign road permissions and i'm going to say us dollar sign roll permission sorry permissions as road permission and here right now i'm going to add this one and display the road permission name roll permission name and maybe add glass here so margin top to be four and padding two okay save from here and yeah here we have the royal permissions but we don't have any role okay let's add here the form and i'm going to copy this form and paste it here sorry paste it here just select all and to format this okay now if i go to the browser and refresh yeah we have this form here but these two counts where is the form with that class that is diff so max width is going to be excel and refresh yeah now here i want to add a drop down uh select and i'm going to copy this one i prepared this so just copy this one and go to the vs code now here when we have this paste and we don't need the country so select all country and say permission and in the label we need to say with capital p permission and here we need to display all the permissions but if i save and refresh yeah now we don't have right now all the permissions so we need to open the role controller and in the edit form in the edit method we need to say dollar sign permissions is going to be with permission and make sure you import the model okay the model is here spotty permission models and permission and say all here just get all the permissions and added them here permissions okay now we have permissions in here and make a forage for each dollar sign permissions as the permission and for that i added your role permission not permission because if i had permission here and also permission here is going to mix up so let's remove that okay add option we need the value and the value is rendered is going to be permission name and here permission name save and let's see and we have two permissions okay now when i click update just also we need to change this not update but uh i'm going to say assign and when we click here i want to attach a new role give a permission to this role and for that we need to change the form road which is admin roles update but it's going to be admin roles permission permissions and i'm going to say add or just permissions permissions save now open the web roles because we don't have that and after the row say road post method is going to be paused and it's going to be rows and the role id and permissions the the controller is going to be your old controller i'm going to add that here class and the method i'm going to say give permission now the name is this name we added here so arrows permissions copy that and say name and paste that okay save now copy this name here and go to the role controller and create that so function give permission and what we need here we need the request dot our sign request and also the road so rover dollar sign road now first when we check here and say a sign we need to check if we have this road if the if the road has a permission we have here in the request so let's say here role has permission to do something and say if the role has permission though and here we need to add the request and permission okay because in the edit we say option value permission here select with name permission okay and now we add that permission here if we have if this row has that permission we need to return back copy this simplicity and i'm going to say permission exists save that's it if we have but if we don't have we need to assign so we need to say roll give permission and the permission and say that give permission and the request permission and after that let return back and say permission added or what you can say a permission add that okay let's check now if i go to this and refresh let's see the update post click assign and okay here we have because i copy that form and we add we have in the form we have this method put but we need post not put go back and refresh select this and assign and permission added and we see here now this updated post this is the permission uh let's as sign another one okay permission added and here it is now another one now i want also here to when i click i want to remove that permission and i'm going to do the same thing we have here with this button and delete i'm going to do that the same thing are the method perform here and remove that so let's open the rows index and here we have that form copy close this one now and here we display the spawn and just i say spa and forget that anyway remove now that and paste it for now in this button remove the delete and display the row permission name save and refresh and we have something like this in the div add glass flex and also a space x the bicho okay i refresh and yeah now we have something like this and maybe in the form our imagine talk to be six because it's very close so refresh and now it's more okay so here it is now if i click i want to remove that load the permission from road and let's see what we can do here use the method and the action the method is going to be delete okay now the action is going to be the admin roles uh and or evo i'm going to say okay and here we need also the role id and the permission id are signed not permission but road permission road permission id ok save and create this road permissions revoke road and i'm going to copy this and paste that route is going to be deleted rows permissions and the permission here roll controller is going to be revoked now not so revoke permission and the name is going to be parallels permissions revoke so copy that and paste it here hit save and now copy this name and create a new method so function with that name here now we need the role and the permission assign road permission dollar sign permission and what we need here so first if we have that we need to check if we have that permission to disrupt so let's say if the road has this permission we have here we need to revoke that and just i'm going to copy this one to save some time paste that in remove the request because we have the permission here and instead of give we need to say revoke okay so if i go here yeah here we have revoked permission okay and after that we return back with message you make permission revoked if not we need to return back copy that and say with massage permission not exist let's save and see what we got go to the browser refresh yeah if i click the article are you sure yeah and permission revoked and also we update here click again here okay and permission revoked again so that's it friends we can assign and revoke permission to the road hope you enjoy friends if you like like the video subscribe to my channel and see you in the next one all the best friends hello friends welcome in this video i'm going to work with the permissions so in the previous video we worked with roads and we can assign permission to the role now i'm going to do the same thing for permission so if we go here down here i'm going to display all the rewards this permission has and also a form to assign a new role and if you don't want to do that no problem because all the relationship we may we made here so it's not a problem but just i'm going to show you how we can do and one more thing here i'm going to add a class to separate this from the form because it's not looking very good so in here let's open the resources views admin roles and edit and now here i'm going to add padding of true and vg to be slate 100 and the same thing down there so bg slate 100 let's save and refresh and yeah now i think it's more okay now let's copy this thing here and go to the permissions edit and paste that in okay let's indent it and here is going to be roads now also down there we need say dollar sign permission rolls and i'm going to leave it as it is or all permission or if you want let's change this let's say permission relevant now the road is going to be permissions roads and remove not revoke but remove and also down there is going to be permissions roles permissions dot roles and here now we need the permission id not role id so permission id okay also here is going to be roles and make selecting them all so let's select from here and say roll and here we have this error let's say for a road but we're not validating or we can validate a problem and let's save it and let's open the web road and create two routes here so first one is going to be i'm going to add it up after the permissions so road and post post permissions and we need the permission here and robots okay now the controller is going to be permission controller class and the method uh is going to be assign load and give it the name so name name is going to be that name we have here so permission rules okay let's put is that in and copy and duplicate this now the second is going to be delete rows and we need the role here now the method is going to be removal and the name is going to be uh permissions roles removed let's copy this and paste in the name let's save and open the browser and refresh if i go now to the permissions edit permission and we don't have the role id in if permission roles so let's go to the edit yeah here we have our id what we need permission id okay save refresh and we don't have the rows so let's open the permission controller and in the edit we need the say dollar sign rules and get all the load sorry all and we need to import that parallel so import spot permission models role and say roads here save refresh and yeah here it is rolls and we have one roll by default because we assign it to the rows and i want also that same background in this one so let's say padding 2 and bg slate 100 save and refresh okay and also why is post name there in the form is not post name but is a permission name okay you can do that but just to be more good now when i assign a new role so here we have all the rows and i want to assign this one we need to create that method and assign a new road we have this assign role we need to create this in permissions oh sorry let's create that function with that name and here we need the request dollar sign request and permission dot assign permission and here we are going to do the same thing we have done with the road controller so here first we check if we have that and return with a message permission exist and after that if we if we don't have we give permission so and here i'm going to say if the permission as role and the request role now if we have that we can say roll exist okay now if we don't have we need to assign so let's say dollar sign permission assign role and the request roll and also after that i'm going to say roll assign okay let's save this and refresh here let's say i want to add the writer to this permission click assign roll assign it and we have the role here very good next thing is when we click here we want to remove that and if i do now we don't have that remove row so let's go and create okay so here let's say function and the name is remove roll here we need the permission the assign permission and the role dollar sign load and after that we need to check if the permission has rolled and add the roller we need now to remove our permission remove road and the road we have and return back with message roll removed otherwise we return with message roll does not exist and save let's go and refresh click on the user click ok and row removed now click and assign user here okay let's assign again the user we have the user assign and roll exist okay so that's it how we can assign roles to the permissions and now here we have this edit post permission has writer and user so if i go to the writer we have edit post and the user has the edit post so writer has edit post also if i go back and see that and this one has edit post but if i go to this delete post no one we have here and if i go to the u writer and give it permission to delete post okay and if i go here now to the delete post we have the role here hope you enjoy friends and see you in the next one all the best don't forget to like and subscribe my channel thank you very much hello friends welcome now in this video i'm going to add link here for users and also display the users in the table the user's index so let's open the vs code and in the web routes let's create or first i'm going to create a new controller for the users so let's say artisan php artisan make controller and is going to be inside the admin slash user controller and after that let's come here and say road and it's going to be route resource or just the road get for the let's say get get users and here at the user controller now the method is going to be index and the name is going to be user's index okay let's save this and in the admin i'm going to create a new folder there users and in their new file index.date.php so so index.blade.php in here now i'm going to copy what we have in the rows index select all them oh sorry select and copy and close this and paste it here now we don't need to create a user i just want to show the users and assign a role okay here we have now we need to have users not rows so select this one and say user and let's say we want to display the name and the email okay and also here now copy this td and display the email now this link is not going to do the edit but for example i'm going to say roles and let's copy one more duplicate this and say permission ok now if we want to delete that the user let's say admin users destroy and add the user id and i think we're okay let's save this go to the browser and refresh we need also to add the link in in the menu so let's go to the layout in the admin layout we have let's copy one of the links and duplicate it and say here users okay now the road is going to be admin users index save refresh and here we have the users if i go here we have the user controller does not exist because in the web routes we don't import the user so let's import that and save and refresh now the user controller index not exist let's open that user controller and say in here function index we don't need anything here just return all the users so users is going to be user oh and i think i want to remove that tab 9 because it's not allowing me to import models and return return the view admin users.index and add the users there so compact users let's save go to the browser and refresh admin user destroy not exist yes let's go and create that so copy this duplicate users delete and the method is going to be destroy and user destroy okay save refresh and now here we have that we have only one user so if i got let's log out from here and create another user let's go and register and say test test and the email is going to be okay let's log out and go and login as admin now go to the admin users and yeah we have now two uh users so that's it for this video in the next video we are going to work with the roles and their permissions all the best friends thank you very much hello friends welcome in this video now i'm going to work with robots and permissions for the user and here i have added two links but i'm going to remove one because i want to add the roles and permission in one page for the user so here in the roles we have permission so in the user we can have you the user details here down there the road and after the permissions okay so let's open the vs code and first first we need to create in the users new file and i'm going to name it a show dot or we can name roll roll dot blade dot php okay in here now i'm going to copy what we have in the edit of roles select all copy and paste it here and also if i go to the permissions edit uh let's copy this here we have the permissions let's copy this and add it down there so let's say here we have the form here we have the rows and down there is going to be the permissions now let's work one by one we don't need to go oh yeah we can say users index to go back and here we don't need the form so let's remove this and just add the div we display here the user name and let's copy and duplicate this and display the email of the user and you can display what you want after that here now we have roles first let's add first rows because here is the permissions and here we use the rows yeah that's going to cut and paste it right here the roles now here is the rows and not permission rules but we need to say uh user roles so user okay now [Music] the route is going to be users roles remove that's okay next one is going to be admin users roles and here we need the user id we need all the roles here so okay that's it now work with permissions here is going to be permissions like this and not all permissions but select all of them and say user permissions okay now the route is going to be admin user permissions revoke okay also here it's going to be user users permissions and the user id okay here we need to display all the permissions and i think we are okay with the blade let's go to the web roads and create one by one the routes so first we need to create this user's roles remove and user roles and here we have users destroy let's say road and post users slash we need user here and say roles now here we need the user controller class and the method is going to be assign role or you can name it what you want the name is going to be what we name it here users rows i think this is for the post add copy and duplicate this and here now we need for delete to remove role and we need users roles and the role itself the method is going to be now remove road and the name is users rows remove let's copy this and add it here okay next is the create for the permission so let's copy this one and add it down there and instead of rows rename this to be permissions we can say here now give permission okay and user permissions the second one the same thing say permission here permissions we need the permission here and revolve permission reform permission and the name is going to be so where is the permission user's permission another one is user's permissions revoke let's copy this go to the web user's permission revoke okay save this and now we have user syntax and user destroy but we don't have the users show so let's create one road good because right now if i go to the users here if i click we need to go to the user's show page or to show the user and add assign rows and permission so i'm going to say users slash and we need the user here and the controller is user controller class the method is going to be show and also the name is going to be users dot show let's save this let's open the user's index and here when we have rows and permissions that remove the permissions and say only rows there and the anchor tag at the road go to the users dot show and we need to add the user here sign user id let's save come here and refresh we have only roles if i click here now we don't have that show method in the user controller let's open the user controller and create that method so let's create function and the method is show we need the user here and we are going to return the royal blade so in here we need the permissions and the all the permission and all the roads okay so let's say here dollar sign roles is going to be with the road and import spotty permissions models role all and also the permissions is going to be permissions import the model from spotty and say all and here and return the view which is uh admin users role and say compact for user so first without user the roles and the permissions let's save this and refresh and yeah now here is the user details down there is the roles of this user health and the permissions and uh if i go here why we add it let's add this one and this so the bg white uh cut them and add it in here because i yeah now we are okay and also change this a bit not all index but uh user's index and i think we're okay so here is the username and the user email and let's say here user name something like this and down there the user email just see more got here okay now when i click here and assign a new user we need to assign a new role we need to have this method inside the user controller so let's copy the name go to the user controller or let's open the permission controller and copy this assign role and remove road because it's going to be almost the same so copy and in the user controller less space in here just inside instead of permissions here we need the user and say user okay and also in here we need to say user now if the user has their old request load we say all exist otherwise we assign a loan in the request and that's it remove road also here we need the user not the permission and say user and also select all and say user here and the same thing now if we have a role if the user has a role we are going to remove otherwise we are going to return back and say or not exist and save and refresh and you'll see here also we are in the admin and we have admin and writer if i add also the user and click sign that row will assign it and we have also the user okay let's say remove this and we removed now let's work with permissions and i'm going to open the road controller and copy them the give permission and revoke permission here okay in the user controller and this is this is because i have named in the web the same name so here assign it all and the same thing in the permission source and now okay if you change the name here make sure you updated the name and now instead of the road we need to save user here and the last one user and change the user here now if the user has permission to return back otherwise we give permission and the same thing here now not at all here we need the user and user here and also remove this and say user so if the user has permission we revoked that otherwise we say permission does not exist let's save and check refresh now here we have it and delete pause if i select delete post assign yeah we see here a permission exist and if i select edit post and why is that permission exist we don't see the permission here but if you go to the roles so this user admin user has to road admin and write if i go to the role and say the writer here this writer has edit and delete both permissions if i remove them okay and go to the user admin and now if i select edit post assign we assign that okay you see here if we if the user has the role and the roller has that permission is not going to assign here because the user has already that permission so only if the this role does not have the permission you can assign them so also assign the delete and if i delete i revoke that click ok and we revoked okay friends uh that's it hope you enjoy and the last one let's also work with delete i think you can do that but just in case let's go to the index where we have the delete admin users destroy we add the user id and delete now in the user controller we need to create the delete destroy method and i'm going to add it here so function and say destroy we need the user here the assign user and user and say user delete and return back so or first i'm going to check if if the user is admin if dollar sign user has role and the role is named admin i'm not i'm going to return back there i'm going to return back and with message so let's copy this and paste it here with message your your admin admin otherwise we are going to delete that and return back with message user deleted okay let's save from here and refresh let's delete this test user are you sure okay and why we have this url you mean i think we'll give it admin yeah remove this admin and go to the users and click delete and click ok now we delete it okay friends hope you enjoy and don't forget to like and subscribe my channel all the best and see you in the next one thank you very much
Info
Channel: Code With Tony
Views: 60,016
Rating: undefined out of 5
Keywords: laravel, vuejs, livewire, laravel 8, laravel 9, laravel crud, laravel 8 tutorial, laravel 9 tutorial, laravel permissions, laravel permission, laravel permissions roles, laravel permissions spatie, laravel permission package, spatie laravel permission, laravel permission laravel 8, laravel permission laravel 9, laravel roles and permissions, full laravel spatie permission, laravel role and permissions, laravel auth roles permissions, laravel 8 authentication
Id: cNrMdCXNml8
Channel Id: undefined
Length: 142min 51sec (8571 seconds)
Published: Wed Mar 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.