PHP code example of gaba93 / laravel-menu-updated

1. Go to this page and download the library: Download gaba93/laravel-menu-updated library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gaba93 / laravel-menu-updated example snippets




'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
	
	...
        
        'Lavary\Menu\ServiceProvider',
        
        ...

],



'aliases' => [

    'App'       => Illuminate\Support\Facades\App::class,
    'Artisan'   => Illuminate\Support\Facades\Artisan::class,
    ...
    'Menu'       => 'Lavary\Menu\Facade',

],


Menu::make('MyNavBar', function($menu){
  
  $menu->add('Home');
  $menu->add('About',    'about');
  $menu->add('services', 'services');
  $menu->add('Contact',  'contact');
  
});

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

{!! Menu::get('MyNavBar')->asUl() !!}


// ...
$menu->add('About Us', 'about-us');
// ...



// Suppose we have these routes defined in our app/routes.php file 

//...
Route::get('/',        array('as' => 'home.page',  function(){...}));
Route::get('about',    array('as' => 'page.about', function(){...}));
//...

// Now we make the menu:

Menu::make('MyNavBar', function($menu){
  
  $menu->add('Home',     array('route'  => 'home.page'));
  $menu->add('About',    array('route'  => 'page.about'));
  // ...

});



// Suppose we have these routes defined in our app/Http/routes.php file

// ...
Route::get('services', 'ServiceController@index');
//...


  // ...
  $menu->add('services', array('action' => 'ServicesController@index'));
  // ...



Menu::make('MyNavBar', function($menu){
  
  $menu->add('Home',     array('route'  => 'home.page'));
  $menu->add('About',    array('route'  => array('page.about', 'template' => 1)));
  $menu->add('services', array('action' => array('ServicesController@index', 'id' => 12)));
 
  $menu->add('Contact',  'contact');

});


	// ...
	$menu->add('Members', 'members')->link->secure();
	
	
	// or alternatively use the following method
	
	$menu->add('Members', array('url' => 'members', 'secure' => true));
	
	// ...



Menu::make('MyNavBar', function($menu){

  //...
  
  $menu->add('About',    array('route'  => 'page.about'));
  
  // these items will go under Item 'About'
  
  // refer to about as a property of $menu object then call `add()` on it
  $menu->about->add('Who We are', 'who-we-are');

  // or
  
  $menu->get('about')->add('What We Do', 'what-we-do');
  
  // or
  
  $menu->item('about')->add('Our Goals', 'our-goals');
  
  //...

});
  


  // ...
  
  $menu->add('About',    array('route'  => 'page.about'))
		     ->add('Level2', 'link address')
		          ->add('level3', 'Link address')
		               ->add('level4', 'Link address');
        
  // ...      
  

	//...
	$menu->add('About',    array('route'  => 'page.about'));
	$menu->add('Level2', array('url' => 'Link address', 'parent' => $menu->about->id));
	//...


	// ...
	$menu->add('About', array('route' => 'page.about'))
	     ->id('74398247329487')
	// ...


	// ...
	$menu->add('About', array('route' => 'page.about', 'id' => 74398247329487));
	// ...


	// ...
	$menu->add('About', array('route' => 'page.about'))
	     ->nickname('about_menu_nickname');
	     
	// And use it like you normally would
	$menu->item('about_menu_nickname');     
	     
	// ...


	// ...
	$menu->add('About', array('route' => 'page.about', 'nickname' => 'about_menu_nickname'));
	
	// And use it like you normally would
	$menu->item('about_menu_nickname');    
	// ...


	// ...
	
	$menu->itemTitleInCamelCase ...
	
	// or
	
	$menu->get('itemTitleInCamelCase') ...
	
	// or
	
	$menu->item('itemTitleInCamelCase') ...
	
	// ...


    // ...
	
	$menu->add('About us', 'about-us')
	
	$menu->aboutUs->divide();
	
	// or
	
	$menu->get('aboutUs')->divide();
	
	// or
	
	$menu->item('aboutUs')->divide();
	
	// ...


// ...
$about = $menu->add('About', 'about');
$about->add('Who We Are', 'who-we-are');
$about->add('What We Do', 'what-we-do');
// ...


	// ...
	$menu->find(12) ...
	// ...


	// ...
	$menu->all();

	// or outside of the builder context
	Menu::get('MyNavBar')->all();
	// ...


	// ...
	$menu->first();

	// or outside of the builder context
	Menu::get('MyNavBar')->first();
	// ...


	// ...
	$menu->last();

	// or outside of the builder context
	Menu::get('MyNavBar')->last();
	// ...


	// ...
	$aboutSubs = $menu->about->children();

	// or outside of the builder context
	$aboutSubs = Menu::get('MyNavBar')->about->children();

	// Or
	$aboutSubs = Menu::get('MyNavBar')->item('about')->children();
	// ...


	// ...
	if( $menu->about->hasChildren() ) {
		// Do something
	}

	// or outside of the builder context
	Menu::get('MyNavBar')->about->hasChildren();

	// Or
	Menu::get('MyNavBar')->item('about')->hasChildren();
	// ...


// ...
$aboutSubs = $menu->about->all();
// ...



	// ...
	$subs = $menu->whereParent(12);
	// ...


	// ...
	$menu->add('Home',     '#')->data('color', 'red');
	$menu->add('About',    '#')->data('color', 'blue');
	$menu->add('Services', '#')->data('color', 'red');
	$menu->add('Contact',  '#')->data('color', 'green');
	// ...
	
	// Fetch all the items with color set to red:
	$reds = $menu->whereColor('red');
	


$reds = $menu->whereColor('red', true);


	// ...
	$menu = Menu::get('MyNavBar');
	// ...


	// ...
	$menus = Menu::all();
	// ...


	// ...
	$menus = Menu::getCollection();
	// ...


Menu::make('MyNavBar', function($menu){

  // As you see, you need to pass the second parameter as an associative array:
  $menu->add('Home',     array('route'  => 'home.page',  'class' => 'navbar navbar-home', 'id' => 'home'));
  $menu->add('About',    array('route'  => 'page.about', 'class' => 'navbar navbar-about dropdown'));
  $menu->add('services', array('action' => 'ServicesController@index'));
  $menu->add('Contact',  'contact');

});


	//...
	$menu->add('About', array('url' => 'about', 'class' => 'about-item'));
	
	echo $menu->about->attr('class');  // output:  about-item
	
	$menu->about->attr('class', 'another-class');
	echo $menu->about->attr('class');  // output:  about-item another-class

	$menu->about->attr(array('class' => 'yet-another', 'id' => 'about')); 
	
	echo $menu->about->attr('class');  // output:  about-item another-class yet-another
	echo $menu->about->attr('id');  // output:  id
	
	print_r($menu->about->attr());
	
	/* Output
	Array
	(
		[class] => about-item another-class yet-another
		[id] => id
	)
	*/
	
	//...


  // ...
  $menu->add('About', 'about');
  
  $menu->about->add('Who we are', 'about/whoweare');
  $menu->about->add('What we do', 'about/whatwedo');
  
  // add a class to children of About
  $menu->about->children()->attr('class', 'about-item');
  
  // ...


Menu::make('MyNavBar', function($menu){

 //  ...
  
  $about = $menu->add('About',    array('route'  => 'page.about', 'class' => 'navbar navbar-about dropdown'));
  
  $about->link->attr(array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown'));
  
 // ...
  
});


	// ...
	$menu->add('Home', '#')->active();
	// ...
	
	/* Output
	
	<li class="active"><a href="#">#</a></li>	
	
	*/
	


	// ...
	$menu->add('Home', '#')->link->active();
	// ...
	
	/* Output
	
	<li><a class="active" href="#">#</a></li>	
	
	*/
	


	// ...
	'active_element' => 'item',    // item|link
	// ...



// ...
$menu->add('Articles', 'articles')->active('this-is-another-url/*');
// ...


	//...
	$menu->add('Separated Item', 'item-url')->divide()
	
	// You can also use it this way:
	
	$menu->('Another Separated Item', 'another-item-url');
	
	// This line will insert a divider after the last defined item
	$menu->divide()
	
	//...
	
	/*
	Output as <ul>:
	
		<ul>
			...
			<li><a href="item-url">Separated Item</a></li>
			<li class="divider"></li>
			
			<li><a href="another-item-url">Another Separated Item</a></li>
			<li class="divider"></li>
			...
		</ul>
		
	*/



	//...
	$menu->add('Separated Item', 'item-url')->divide( array('class' => 'my-divider') );
	//...
	
	/*
	Output as <ul>:
	
		<ul>
			...
			<li><a href="item-url">Separated Item</a></li>
			<li class="my-divider divider"></li>
		
			...
		</ul>
		
	*/


Menu::make('MyNavBar', function($menu){

  // ...
  
  $about = $menu->add('About',    array('route'  => 'page.about', 'class' => 'navbar navbar-about dropdown'));
  
  $menu->about->attr(array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown'))
              ->append(' <b class="caret"></b>')
              ->prepend('<span class="glyphicon glyphicon-user"></span> ');
              
  // ...            

});


    // ...
    $menu->raw('Item Title', array('class' => 'some-class'));  
    
    $menu->add('About', 'about');
    $menu->About->raw('Another Plain Text Item')
    // ...
    
    /* Output as an unordered list:
       <ul>
            ...
            <li class="some-class">Item's Title</li>
            <li>
                About
                <ul>
                    <li>Another Plain Text Item</li>
                </ul>
            </li>
            ...
        </ul>
    */


Menu::make('MyNavBar', function($menu){

  $menu->add('Home',     array('route'  => 'home.page', 'class' => 'navbar navbar-home', 'id' => 'home'));
  
  $menu->group(array('style' => 'padding: 0', 'data-role' => 'navigation') function($m){
    
        $m->add('About',    array('route'  => 'page.about', 'class' => 'navbar navbar-about dropdown'));
        $m->add('services', array('action' => 'ServicesController@index'));
  }
  
  $menu->add('Contact',  'contact');

});


Menu::make('MyNavBar', function($menu){

  $menu->add('Home',     array('route'  => 'home.page', 'class' => 'navbar navbar-home', 'id' => 'home'));
  
  $menu->add('About', array('url'  => 'about', 'class' => 'navbar navbar-about dropdown'));  // URL: /about 
  
  $menu->group(array('prefix' => 'about'), function($m){
  
  	$about->add('Who we are?', 'who-we-are');   // URL: about/who-we-are
  	$about->add('What we do?', 'what-we-do');   // URL: about/what-we-do
  	
  });
  
  $menu->add('Contact',  'contact');

});


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');
		});
	});
	
});


Menu::make('MyNavBar', function($menu){

  // ...
  
  $menu->add('Users', array('route'  => 'admin.users'))
       ->data('permission', 'manage_users');

});


	
	//...
	
	$menu->add('Users', '#')->data('placement', 12);
	
	// you can refer to placement as if it's a public property of the item object
	echo $menu->users->placement;    // Output : 12
	
	//...


  // ...
  $menu->add('Users', 'users');
  
  $menu->users->add('New User', 'users/new');
  $menu->users->add('Uses', 'users');
  
  // add a meta data to children of Users
  $menu->users->children()->data('anything', 'value');
  
  // ...


Menu::make('MyNavBar', function($menu){

  // ...
  
  $menu->add('Users', array('route'  => 'admin.users'))
       ->data('permission', 'manage_users');

})->filter(function($item){
  if(User::get()->can( $item->data('permission'))) {
      return true;
  }
  return false;
});


Menu::make('main', function($m){

	$m->add('About', '#')     ->data('order', 2);
	$m->add('Home', '#')      ->data('order', 1);
	$m->add('Services', '#')  ->data('order', 3);
	$m->add('Contact', '#')   ->data('order', 5);
	$m->add('Portfolio', '#') ->data('order', 4);

})->sortBy('order');		


Menu::make('main', function($m){

	$m->add('About');
	$m->add('Home');
	$m->add('Services');
	$m->add('Contact');
	$m->add('Portfolio');

})->sortBy('id', 'desc');		


Menu::make('main', function($m){

	$m->add('About')     ->data('order', 2);
	$m->add('Home')      ->data('order', 1);
	$m->add('Services')  ->data('order', 3);
	$m->add('Contact')   ->data('order', 5);
	$m->add('Portfolio') ->data('order', 4);

})->sortBy(function($items) {
	// Your sorting algorithm here...
	
});		

{!! $MenuName->asUl( array('class' => 'awesome-ul') ) !!}

  {!! $MenuName->asOl() !!}

{!! $MenuName->asOl( array('class' => 'awesome-ol') ) !!}

  {!! $MenuName->asDiv() !!}

{!! $MenuName->asDiv( array('class' => 'awesome-div') ) !!}


Menu::make('MyNavBar', function($menu){
  
  $menu->add('Home');
  
   $menu->add('About',    array('route'  => 'page.about'));
   
   $menu->about->add('Who are we?', 'who-we-are');
   $menu->about->add('What we do?', 'what-we-do');

  $menu->add('services', 'services');
  $menu->add('Contact',  'contact');
  
});

<nav class="navbar">
  <ul class="horizontal-navbar">
    @

@foreach($items as $item)
  <li @if($item->hasChildren()) class="dropdown" @endif>
      <a href="{!! $item->url() !!}">{!! $item->title !!} </a>
      @if($item->hasChildren())
        <ul class="dropdown-menu">
              @

...
@

@foreach($items as $item)
  <li @if($item->hasChildren()) class="dropdown" @endif data-test="test">
      <a href="{!! $item->url() !!}">{!! $item->title !!} </a>
      @if($item->hasChildren())
        <ul class="dropdown-menu">
              @


// ...
$menu->add('Dropdown', array('class' => 'item item-1', 'id' => 'my-item'));
// ...

@foreach($items as $item)
  <li@lm-attrs($item) @if($item->hasChildren()) class="dropdown" @endif data-test="test" @lm-endattrs>
      <a href="{!! $item->url !!}">{!! $item->title !!} </a>
      @if($item->hasChildren())
        <ul class="dropdown-menu">
              @


return array(
	'default' => array(
		'auto_activate'    => true,
		'activate_parents' => true,
		'active_class'     => 'active',
		'active_element'   => 'item',    // item|link
		'restful'          => true,
	),
	'yourmenuname' => array(
		'auto_activate'    => false
	),
);


// ...
$menu->add('About')->link->href('#');
// ...
html
  {!! $MenuName->asUl() !!}