so welcome to the Laravel permission package tutorial this package is
supremely famous why because it has more than 1 million downloads this package is
created back spatie that means the person behind this path he is freak and
you know freak he is just like a package king because he has more than 200
package just for level so freak is very good on that and because this is a super
famous package so why not get into this you have probably used this package but
if you have not used follow me for inside of this package and if you
have used maybe you can find something new so let's get started
so here we can see we need to firstly grab this package by using the composer
and obviously for that we need a level project and for that I already have
level 5.7 fresh installation so if I go to the vs code you can see this is the
this is the level app and I'm just going to get this speci level permission
package by composer so while it's downloading let's create a new terminal
integrated terminal off vs code and I'm going to say get in it so that it will
initialize the kit in inside this project and let's now go to github to
create a new repository for this so I'm going to create a new repository that
repository will be inside the bit films and I will say speci level permissioned
tutorial super easy so I will fill up the description later so create the
repository and now I just need to commit and add the remote repository so go here
gate and let's just see its completed no no problem so git add commit with a
message of initial simple thing and now I'm going to add a remote repository and
then git push origin super easy steps and you
can see it is there and if i refresh yes all these things are here okay
so let's now see and yeah you can see this is completed and because of this
now we have the speci level permission package here
okay so let's first see how we can start with this after installing we just need
to publish the migrations and let's now publish the migrations and if I go to
database migrations you can see we just have two tables migration if I run it I
have another but if you go inside that you will have a one schema for
permission stable then for role stable then mod model has Commission table then
module has rules table and role has permission and the final one permission
table so all these things are here let's now connect our project with a database
called test and everything are just two pretty basic things and you can see the
test database is empty so I'm going to migrate so PHP artisan migrate simple
and yeah it is done so if I go refresh all the tables are here let's now go to
Firefox for the next thing yeah we have done that and now we need to publish the
configurations so let's see the configurations so this configuration is
inside our config folder so if I go inside the config folder you can see the
new file is permission dot PHP this is the one which is just published so now
let's look inside this file so first thing it's having the models so all the
models related to this package are permission and the exact model is here
so this means you can change if you want so suppose you
don't want to use the model of the package you want your models you can
just define your model class here similarly for rule and then if you want
to change the name of the tables you are going to use or your package is going to
use you can simply change the rule and permission and all the table needs
then comes the column name column name is for morphing that if you are familiar
with eloquently a moth many then it is very useful and if you don't know just
to check out the relationship on my level five point four series it's the
same for five point seven so no worries then comes the cashing time so because
this package cash the permission and rules I will show you how it's going to
cash but just for now it's just going to cash it and it's going to cash all the
permission and roll for 24 hours if you want to change the expiration time of
the cashes then you can define that here and obviously this has to be in minutes
so 24 hours x 60 minute every our so this is this much amount of minutes then
comes the display permission in exception
suppose you are having any kind of exception related to the permission so
do you want to display that permission named on the exception if yes then
change it to yours but obviously if your app is used for some other person you
don't want to display or disclose your permissions related to your admin
sections so that's all about this permission dot PHP file or simply call
it config file let's now move on to the next thing let's scroll down and we are
going to see some kind of things for lumen then yeah how we can use this with
any existing model we have so we have the user model and we just need to have
this has rules traight so let's go to vs code once more
and go to the user model and here just like we have notifier
notifiable trade we are going to use has role and yep we need to import it also
so we have done that let's cool and if we if you are using other than any model
which is not authentic a table model then you need to define this guard name
of web or whatever you are having so now we just need to see how we can create
the role and permission it's very easy let's just try to do one thing I'm going
to say PHP artisan make auth so that I have a login ID system in it and in that
way if I go to let's start the server to so PHP artisan serve and if I now go to
localhost 8080 or a user so let's say register quickly and we are registered
and now let's do one thing here we have user is there and now what
I am going to say I'm going to create a new role so to create a new role which
is very easy let's just go to the HTTP controller home controller and here we
are going to create a role so role and the role model actually then create role
name is let's say write a simple one so we have done that and obviously this is
giving error because I need to make this inside this square brackets so let's
refresh nothing is displayed here but if I go here refresh the page yes we have
one rule of writer similarly we can create a permission so I can just say
instead of role I will say permission and permission create and permission
name is right post so it will be right post and once more
let's go refresh the page nothing is there but now
inside the permissions table we have to post that's very easy and exactly given
help these things are here okay now we need to connect that hole and the
permission because a roll is writer so writer can write the post so we need to
connect these two so that if user is assigned the role of writer obviously he
can write the post so let's go here and again comment this so let's grab the
role so I will say role is equal to role and find with IDs so find by ID because
we only have one roles so that's why I am getting from one then I will say role
and will to assign permissions or give a permission so give permission so give
permission to and which permission we need to get the permissions also so it
will be like for mission is equal to permission and find by ID again and we
know it's still one and giving the permission here so now if I go once more
to here refresh the page go to the sequel pro we have the permission we
have the role but at this time on the role has permission you can see
permission and role are connected so this is the way we can connect role
permission and both of these two so this is nice so yeah we can do the reverse
also we can have have the permission and we can assign a role to it so we have
now permission so let's now create another permission so I am going to
create a permission right or let's say edit post and I'm going to say a writer
can edit a post also so permission is equal to this let's comment this
permission so I am getting the role which is the writer I have created the
new permission which is this fun then I say this role which is the writer give
permission to edit post simple enough so let's go and on the Missoula so let's
refresh and yeah if i refresh two and one so we have created another
permission which is edit post and now that role that writer role has also the
permission of two in that way you can just interchange between role permission
operation role you can assign roles to a permission or audit you can give
permissions to the particular role so this is very nice and you can see if you
want to add more than one permissions to a role you need to use the sink keyword
similarly for the permission more than one role if you are assigning then use
the sink rules and rules though more than one rules
similarly let's now try to revoke the permissions so we have these permissions
so Commission has the permission has the role so we can say revoke the role so
let's say we are having the role and now I am going to say permission remove role
and the role name so simple enough and yeah we have this refreshed page and
okay we have edit post permission already exist for the web so yeah it's
because you can see this is giving exception because we already have the
theme permission and we are trying to create that and that's why it's giving
the error that okay edit post permission is already there so refreshments moon
and undefined variable so because we don't have the permission so let's grab
the permission so I think we have yeah here we have so let's try to get the
second one and once more try and no error this means now the permission and
the role relationship for to net permission ID to is gone
so similarly if you want to remove the permission for it all we have just
removed the rule for function let's remove so we have roll and revoke
permission and the permission like this so let's grab the permission of one and
let's check out the exact thing Oh permission to so that has to be
permission to so like this okay so let's once more go refresh the page and
everything is gone no relationship between permission and the role so these
things are very good and in the next episode we are going to see how we can
use these permission and rules on our model that means a user model because we
have given the trait of has rules so if you have liked this episode please go
and subscribe to this channel for more level related updates and don't forget
to like pet films on Facebook Twitter and Instagram we will meet in the next
episode till then good bye