**The content of this video is intended for
educational and entertainment purposes.** **It is NOT production ready.** If you're new to the channel, welcome.
If you're not new, welcome back. Today we are continuing on with our Filament admin
panel series and we have Filament installed, but now we need to get some admins into our project.
Filament has suggested that we use Spatie's Laravel-Permissions, and like I said in the
preview episode I think you can potentially use other roles based packages. However, we're going
to go ahead and follow their advice and use this package. Before anyone asks me hey, use Laratrust,
hey use this package, hey use that. Listen, I am working from a list of projects that I have
been building ever since I started my channel, I haven't gotten to it yet. I can't do them
all in one video, and I am working from that list. With that being said, let's take a look at
Laravel-Permission. Spatie's Laravel-Permission is a very popular package in the Laravel community,
and it has been around for a while. It's been tried and tested and it's you know, a fairly
good package. It really does cover most bases on creating a user with roles and permissions,
and creating the relationships between users and roles and permissions. If we head down a
little bit, you can take a look and see some of the examples and how you could do it. One instance
that it has here, is that it has a user variable, and it is giving permission to edit articles
to that user. If you wanted to assign a role, you could also do that with the user, where
you assign role and the role name is writer. You can also give permission to the role
with the name of whatever the permission is. It just goes through different methods which
you can use in your Laravel blade files, where you can allow users to do certain things
based on what their permissions are, or even based on what their roles are. Typically, I like
to create permissions and add them to the roles, and then to users add those roles that have baked
in permissions already. The method I'm going to show you is a method that works for me. You are
not obligated to use it. Use what works best for you. My method will at the very least show
you how they work. Let's go ahead and download this bad boy. If we go over to the installation in
Laravel, make sure that's the one that you click. It'll take us to the rest of the documentation for
installing and setting it up. We need to go ahead and require the Spatie Laravel-Permissions package.
In the terminal, whatever terminal you're using, it's fine. It does not have to be VS
Code, but we're going to open this up. That's what I'm using, and I'm going to go
ahead and paste it in here, and press enter. Okay, now that that's done, let's head back
and continue on. Now if you're using a version, not a recent version. Most people are using
at least Laravel version 8. If you're not, and you're using way way before, maybe before
Laravel 5, then you would have to include this permission service provider class in your
config/app.php, and you can follow those instructions here. But if you're not using
that, then it'll automatically get registered. Now we can go ahead and publish
the migration and the config file, and then there's a note if you're using uuid's
which we are not, but if you are in your project, then you can go ahead and follow these
instructions right here, and then you can clear the config. We don't really need to do that,
but we will need to migrate. So before we do that, let's go ahead and take care of a few things.
The first thing that I want to do is in the users table. I want to create a field for is admin.
We've already migrated it, so what I want to do is add another field, and we can do that by doing PHP
artisan make migration, and then we'll call it add is admin to users table, and let's go
ahead and open up that table. Which, this one is the permissions table that we got
with the install of the Laravel Spatie permission, and if you take a look here there are all
kinds of different tables. One for roles, one for permissions, and then it has the
models that the permissions and the roles have, and they also have the relationships between the
permissions and the roles and the users as well. So in our new table, we already have it right
here, so all we need to do is just add that field. It's going to be table, Boolean, is admin, and
then we want to put it after the name field, and that's already in the database,
and then we want to make the default 0. Then for it to drop this table, well actually
no for it to drop the field from the table, if we re-migrate the table or anything like
that, we want this field to also be included. We're going to do a table drop column, and
then is admin. When we get ready to migrate, then this field will also be included. The next
thing we need to do is go over to our app models user model, and we need to bring in the trait
for that package, and that trait is called use HasRoles, and we should be good there. But before
I do it, I want to also add that field here in the fillable, because that will be something
that I will absolutely most definitely forget, but now we can go ahead and close these out.
Okay, let's go ahead and continue on. This is just talking about the contents in the
config file, which we won't need to change anything. We're going to use the default
settings for all of that. Let's go ahead to database seeding, because I want to create
a seeder for this. Then it instructs us to flush the cache before seeding, so I'm going to go
ahead and I'm going to copy this. Well actually, I'm going to copy this whole thing, and
then let's head back to the text editor. In here, we're going to open this up, and we are
going to create that seeder, so PHP artisan make seeder, and I'll call it roles and permissions seeder. Okay, let's go ahead open it up, and then we're going to go ahead and paste
that line that we just copied right here. Okay, so let's drag this over a bit, and we don't need
to import anything because it already has the namespace right here. So to save us some time,
I am going to go ahead and paste this in. If you guys have seen any of my previous admin panels,
this is pretty much how I set up this seeder. If we go up top, we'll take a look. I have made
some changes to it and I've made it so that it is, I think maybe a little less confusing. I know
some people were terribly confused with some of the things I added, and that was my fault. Some
of the things that I had in there, I just really kind of over complicated it unnecessarily. So
this is the new version of that which I created, and I'm going to go ahead and start importing some
of these. We are going to be using the Spatie's model, and the Spatie's model, and the app model,
Illuminate Support, and I think that's pretty much it. If we take a look here, we have reset all of
the cached roles and permissions, which we don't have anyway so it's not a big deal. But if we were
to reseed this, then this would be useful to us. What I did here was, I created all of the
permissions, and I've separated them so that you know what they are. The miscellaneous permission
is just kind of like not applicable, like they don't have any permissions to do anything. We
still want to give them a permission though, but we just want the permission to be explicit
that it's not really a permission for anything, which is only going to go to the standard users,
but I defined it here anyway. I also created some permissions for the user model, which is
to create, read, update the user. I created permissions for the role model, which is to
create, read, update the role. For permissions, I added create, read, update, and delete the
permission. Now I've also set up permissions for admins, and there's only 2. One is to
read the admin, and one is update the admin. Those are only going to Super admins and admins
anyway, but we have still defined them up here. The next thing we've done was to start creating
the admins. Well, actually what this is, is to create roles. So I'll go ahead and fix that
here. We have created a user role. We've given them permission, and we have created the super
admin role. Again, we have synced the permissions to those roles. Same for the admin, same for
the moderator, and same for the developer, and then we've gone ahead and created the user and
just assigned the role. In the previous versions of these, I also added that you sync permissions
to the user. That's only really going to be for, if you create a special permission for that user.
So if we add another permission to just the admin role, then you can sync whatever permissions
or give permission to whatever that permission is. You can still do that here if you want to,
but in order to not complicate it, I just want solely the roles to have permissions and the roles
to be given to users, that's all. I've also done that for the admin, the moderator, the developer,
and then what I did was in the previous versions, I did this in the user seeder that I created.
But instead, we're just going to do it all in the same seeder. We've looped over between 1 and 50,
all of the users. We've given them a name. Every iteration it'll increase, so test 1, test 2, test
3, and that's how they'll be given their email. That's how they'll be given their name. We've
hashed the password, **unintelligble** hashed the password, and then we have the remember token,
and then we've assigned the role of user role. Now let's go ahead and add this to the database
seeder. In here, we're just going to leave these commented out, they're just the defaults that
are given to us. We're going to call that seeder, so I just want to move this up here, and then here
I'm going to do this. Call, and then roles and permission seeder, class. Now this can also be an
array. I know sometimes I will put some brackets here like that to indicate that it's an array, and
you don't really have to do that, if it's just one class but if you are adding more classes to it
then of course you can put the brackets in here. But we know that we only have the one for now, so
I'm just going to go ahead and leave it like that. Now I'm going to go ahead and
do PHP artisan migrate seed. Oh okay, because part of
the seeder that I just had, we already had the admin from the Filament
setup that we did, so we'll just refresh it. Good to go. Let's go ahead and
check out the database now. Cool, so we have the roles has permissions,
and the roles, permissions model has roles, model has permission. Some of these will be empty.
As I said, we didn't sync the permissions to the users directly, so this one will be empty, but we
can go ahead and take a look first at roles. Okay, so we have a name: user, super admin,
admin, moderator and the guard name is web. Permissions, we have create user, read user.
These are all the permissions that we gave, and the guard name is also web. Model has roles. We have the user model,
and each of the user models has roles, and these are the user IDs, and as you can see
the model ID starts off with the 5th user. 5, 6, 7, 8, but we can go ahead and change this. Now you see this is role ID 2, which is super
admin, admin, moderator, developer and this is where the users start at 5. As I said, model has
permissions will be empty because we haven't given the model permissions, just the roles. So now
we have some roles and permissions to work with. If you're enjoying the content, go ahead
and click that like button as it really does help out the channel. Here's
a video YouTube thinks you'll like, and here's a playlist to follow along.
Thanks for watching. I'll see you next time.