v1.1+
The add
method and all convenience methods (link
, html
, action
,...) have an equivalent method with an IfCan
suffix. When used, the item will only be added if the currently logged in user has a certain permission.
Menu::new()
->link('/', 'Home')
->linkIfCan('view-posts', '/posts', 'Posts');
<ul>
<li><a href="/">Home</a></li>
</ul>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/posts">Posts</a></li>
</ul>
If your permission check requires parameters, you can provide an array for the permission. The permission's name should be the first element, and parameters should fill up the rest.
Menu::new()
->link('PostController@show', 'View post')
->linkIfCan(['edit-post', $post], 'PostController@edit', 'Edit post');
<ul>
<li><a href="/posts/1">View post</a></li>
</ul>
<ul>
<li><a href="/posts/1">View post</a></li>
<li><a href="/posts/1/edit">Edit post</a></li>
</ul>