Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel menu based on role?

i have codeigniter background. Now, i'm learning about laravel. So, i'm in this condition (Example), I'm trying to create a webapp, which has multiple users. The UsersType A , they can access menu a, menu b & menu c and UsersType B they only can access Menu a.

Now, i'm using https://github.com/lavary/laravel-menu . Well, if it's only have two types , i can write it manually. But, what if there are five types of user or more.

When i'm using codeigniter. I create 4 table Users , UsersType , Menu & MenuAccess. You must be understand how it's work. I just, play it with query then i show it.

UsersType (Users) -> TypeId (UsersType) -> MenuId (MenuAccess) -> MenuId (Menu)

I already use google and I found this https://github.com/Zizaco/entrust but what i can see from that package. It's only give the permission on the action (input,edit & delete)

So, Can i do my codeigniter way in my laravel ? Save my routes properties than show it in my rouotes/web.php (I don't know if it possible, haven't try it yet). sorry for my english.

like image 501
YVS1102 Avatar asked Jan 29 '23 07:01

YVS1102


1 Answers

What I would do is put a function in the User class which checks it's own permission and then returns a view which contains the menu that user has access to.

public function menu()
{
    switch($this->role) {
        case 'admin':
            return view('menus.admin');
        [etc]
    }
}

Then in the view just check if the user is logged in and show the menu:

@if Auth::check()
    {{ Auth::user->menu() }}
@endif
like image 120
Nathan Heffley Avatar answered Feb 05 '23 15:02

Nathan Heffley