1

I am using Lavary's laravel-menu package, and I am trying to build a simple 2 level menu system based on the contents of a db table. I have the middleware created to create the menu on each request:

public function handle($request, Closure $next)
{
    Menu::make('NavBar', function($menu){
        $menuitems = MenuItem::all();
        foreach($menuitems as $menuitem)
        {
            if(!is_null($menuitem->parent)){
               // For example, 'Conferences', a top level menu item with a null parent field
               $menu->add($menuitem->title, url($menuitem->url));
            }
            else{
                // Parent is a field in database, for example 'Traverse City 2015' would have the parent 'conferences'
                $parent = $menuitem->parent;
                $menu->item($parent)->add($menuitem->title, url($menuitem->url));

            }
        }
    });
    return $next($request);
}

In my view, I call:

{!! $NavBar->asUl() !!}

I would expect this to render as such:

<ul>
  <li><a href="/conferences">Conferences</a></li>
  <ul>
    <li><a href="/conferences/traverse-city-2015">Traverse City 2015</li>
  </ul>
</ul>

Instead, it is rendering:

<ul>
  <li><a href="/conferences/">Conferences</a></li>
  <li><a href="/conferences/traverse-city-2015">Traverse City 2015</a></li>
</ul>

Any ideas why the sub-items aren't showing up correctly?

1 Answer 1

3

What you are after is nested groups: https://github.com/lavary/laravel-menu#nested-groups

Menu::make('MyNavBar', function($menu){
    $menu->group(array('prefix' => 'pages', 'data-info' => 'test'), function($m){

        $m->add('About', 'about');

        $m->group(array('prefix' => 'about', 'data-role' => 'navigation'), function($a){

            $a->add('Who we are', 'who-we-are?');
            $a->add('What we do?', 'what-we-do');
            $a->add('Our Goals', 'our-goals');
        });
    });

});

This will then render as:

<ul>
    <li data-info="test">
    <a href="http://yourdomain.com/pages/about">About</a>
        <ul>
            <li data-info="test" data-role="navigation"><a href="http://yourdomain.com/pages/about/who-we-are"></a></li>
            <li data-info="test" data-role="navigation"><a href="http://yourdomain.com/pages/about/what-we-do"></a></li>
            <li data-info="test" data-role="navigation"><a href="http://yourdomain.com/pages/about/our-goals"></a></li>
        </ul>
    </li>
</ul>
Sign up to request clarification or add additional context in comments.

3 Comments

Using your snippet, my project is rendering <ul><li>About</li><li>Who we are</li><li>What we do?</li><li>Our Goals</li></ul>, which is honestly the problem my code is having, as the documentation for sub-items says it should work.
Hm, I am seeing both the issue you posted and, some time ago this issue: github.com/lavary/laravel-menu/issues/91 . It looks like the feature may not be working, and on top of that there has been no activity on the repo for about 3 months. It might be time to fork it and fix it yourself, or find an alternative package.
Fixed since february '16: github.com/lavary/laravel-menu/issues/91#issuecomment-182247847 Update to latest version of lavary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.