RFFRED
4 months ago
267
1
Laravel

How to pass variable to group function / lavary menu

Posted 4 months ago by RFFRED

Hi,

The following code has been written using the lavary/laravel-menu. It feeds from the database where I have a nested tree.

//if the menu does not exist
if (! Menu::exists('DashboardMenu'))
{

	//create the dashboard enu
	Menu::make('DashboardMenu', function($menu){
		
		//loads the menu from the CMS tree. Descendant of Dashboard
		$cms_pages_tree = CmsPage::descendantsOf(2)->toTree();

		//loops items
		foreach($cms_pages_tree as $page)
		{

			$menu->add($page->label, ['route' => $page->route]);
			
			$menu->group(['style' => 'padding: 0', 'data-role' => 'navigation'], function($m){

				foreach($page->children as $child)
				{

					$m->add($child->label);

				}

			});
			
		}
		
	}

}

I have added the notion of 'Groups' to get data displayed in ULs and LIs. However, I do not know how to pass the $page variable (from the loop) into the $menu->group function

		//loops items
		foreach($cms_pages_tree as $page)
		{

			$menu->add($page->label, ['route' => $page->route]);
			
			$menu->group(['style' => 'padding: 0', 'data-role' => 'navigation'], function($m){

				foreach($page->children as $child)
				{

If I do

$menu->group(['style' => 'padding: 0', 'data-role' => 'navigation'], function($m, $page){

I get

ArgumentCountError
Too few arguments to function RF\RFCmsPages\Http\Middleware\GenerateCmsMenus::RF\RFCmsPages\Http\Middleware\{closure}(), 1 passed and exactly 2 expected

Please sign in or create an account to participate in this conversation.