how to dynamically show hide/menu elements
hi i'm getting this setup and i want to add a new menu element but only show it to certain people? i can add elements to the array in menu.php so they show up, but i wanted to do something like
if(auth()->id == 123){
"special hidden menu item"
}
but this fails because auth is not available and shows this error:
Illuminate \ Contracts \ Container \ BindingResolutionException
Target class [auth] does not exist.
any advice please?
Replies (4)
To dynamically show or hide menu elements in a web page, you can use JavaScript (or a library like jQuery) combined with CSS.
Hi
In Laravel, we have implemented the menu permission using spatie/laravel-permission plugin.
You can check the menu filter code here.
app/Core/Adapters/Menu.php
Add permission or role in the menu config. For example;
array(
"title" => "Overview",
"path" => "documentation/getting-started/overview",
"role" => ["admin"],
), You can create a role in your controller and assign it to the user.
$role = Role::create(["name" => "admin"]);
auth()->user->assignRole("admin"); For more information about the permission plugin, please check the docs here;
Thanks