Laravel Filament Admin Panel: [6] Custom User Menu

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
**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 I want to work on some customization. A lot of you guys have asked me: "How do you customize different things?" There are a few different ways you can do it. I'm going to show you one of the main methods in the documentation, and hopefully you'll be able to figure out whatever kind of functionality you want. At least you'll have a better idea of where to look for it and how to add it to the project. For me, what I want to do is, I want to take these links that we have here and then I want to add them to the user menu over here. As you can see, we have the admin name, we have the toggle for the dark mode, and we also have the sign out link, but what I want to do is have those links listed here as well. Let's head over to the documentation. In the documentation, if you head over to admin panel, and then you scroll down to navigation, this will take you to the top of the page where we are. As you can see here there are different ways of customizing the navigation, and what I want to do is customize the user menu. What this is essentially telling you is that you can use the Filament façade within your project, outside of the Filament folders, and you can customize different things like here is the user menu item, and that's pretty much what we're going to be using. You can also create an account link for if you have an account page, which we do not have but you could make one, and then just set it up like this and then you can also customize the logout link, and just other different things. But this service provider, the good thing about having it is that you can customize different things with it, so let's go ahead and create that service provider. Now if we head over to our terminal, we can go ahead and I want to show you PHP artisan, and this is going to print out different Artisan commands. The one that we're going to be looking for is the make provider, that's what we're going to call it. PHP artisan make provider, and we'll call it Filament service provider. Let's go ahead and check that out. If we go to the app folder, and we go down to providers, you can see that we have a new service provider down here. We can go ahead and close this out, we're not going to need that. In here, basically a provider is just allowing you to bootstrap different things to your project. This is where we're going to register the different functions to customize that navigation menu. Before we do anything, we need to register this Filament service provider in the config app.php, and if we head down to where all of the service providers are listed we can now do app providers filament service provider class. Now that we have it registered, let's go ahead and start working on the service provider because we are using Laravel Spatie's permission, we can leverage some of the functions within it that could search for any role. Okay, so for this boot function we can use that Filament facade, and then we're going to do serving. We're going to do function, and now we'll be able to work in here. Let me put a semicolon there. We're going to have to search for something, so we're going to do if auth user is admin. We're doing this because if a user happens to get to the back end, this is something that could kind of potentially keep them from seeing anything. These are just the parameters that we're setting for this drop down. Auth user is admin. If that equals 1, and auth user has any role. As I said, we are using Spatie's Laravel permission, and this is a function that we can use to call for any particular role. Now the only admin that we have in our system that isn't allowed to really do anything in so far as what we've created. So they can't do anything with permissions. They can't do anything with users, and they can't do anything with the roles. That's something that we can exclude from this list. The first one is the super admin. The next one is the admin. The next one is the moderator, and that is it. Keep in mind, we haven't set up any authorization yet. Users can still go to these pages. We are just dealing with the items in the user menu. We are not dealing with the authorization today. This is just to use the service provider to add some customization. We have checked if the user is admin equals 1, and the user has any role of super admin, admin, or moderator. Now we can go ahead and do the first one. Well, actually we can do Filament register user menu items, and the first user menu item we'll make, and we can add a label for it, and for the label we'll just say manage users. The URL is user resource, and then we can do get URL, and this will allow us to be able to link to the URLs within the user resource, and now we can also add the icon which is the hero icon, solid, users. Okay, and I need a semicolon, and that should be good. Let's go and see if we can see this first link. Okay, great. So you can see we have manage users here. Let's go and add the roles and the permissions and then we'll check to see if the developer will also be able to see these. I'm going to go ahead and throw a comma on that, and then I'm going to copy this, and then I'm going to paste it twice. Now this user menu item can be manage roles, and we need the role resource, and I think I need to import that. The heroicon is solid, and we'll just do a cog for that one. So this one will be permissions permission resource, and the icon will just be a key. Let's see if we can see these now. Look at that. We have manage users, manage roles, and manage permissions. Now let's go ahead and log in as the developer and see if we can see these, because we shouldn't based on the conditions that we set. So let's go ahead and log out. Attempt to read property is admin on null, and as you can see here we're on the admin login page. Filament has no real authorization yet, but it is still part of our app and because we don't have any authenticated user, the auth user is admin is null. What we need to do here is if auth user, and then we'll take this and we'll move it up into that condition, and that should clear up that error because now there's no auth user, so none of this will happen at all. Go ahead and refresh it and now we're back to the login page. Let's go ahead and log into the developer, and then we'll try to log out and make sure that that is still the case. So developer@admin.com, password. We are logged in as the developer. Let's go ahead and click that, and as you can see we do not have any links here. However, we still have these links here which we can get to, and if we click them we can definitely get to that. As you can see, this user is still fully able to see the users, roles, permissions, and the developer is not allowed to see any of that, and they are not allowed to create or delete or edit any of these things. None of these are the developer's role. We have not yet done authorization. When we do, that is when we will take care of that kind of functionality. For now, if you don't want any of these links here, there is something that you can do here, since you already have them up here. Well, at least for the other admins you have those links up here. You can also hide them from here, and so to do that I am going to show you on one of them, and if you want to use it you can go ahead and use it. I just thought it might be something that you could integrate if you wanted to. It's another property, and it's going to be a protected static string, and it is called should register navigation, and that is going to equal false. Again, as I said we're only going to do it for the -- this is actually supposed to be a Boolean. I'm only going to show this to you for the users. If you want to do them for others, you're more than welcome to. This is just a demo, so I just wanted to show you how to do it. So now this link should not be here. As you can see, the link is gone, and the link was never up here. Let's go ahead and log out and we're going to log in as the admin again. As you can see, that link is removed from here but it is still here, manage users. I'm going to comment that out for now, but I'll keep it in the code so that you guys can see it. If you're enjoying the content, please 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.
Info
Channel: Designated Coder
Views: 6,023
Rating: undefined out of 5
Keywords: laravel, laravel 8, designated coder, #dezignatedcoder, designatedcoder, laravel 9, laravel admin panel, filament, filament admin panel, how to create an admin panel in laravel 8 from scratch, how to create an admin panel in laravel 9 from scratch, laravel admin panel from scratch, laravel user management system, laravel multi auth, laravel multiple authentication, spatie permission laravel 8, spatie permission laravel 9, laravel admin panel tutorial, #designatedcoder, soft deletes
Id: mJYjJa0ehx4
Channel Id: undefined
Length: 10min 12sec (612 seconds)
Published: Mon Oct 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.