Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build menu with nested categories from database #295

Open
schel4ok opened this issue May 18, 2022 · 1 comment
Open

build menu with nested categories from database #295

schel4ok opened this issue May 18, 2022 · 1 comment
Labels

Comments

@schel4ok
Copy link

I cannot understand how to build menu with nested categories from database.
I have categories build with laravel-nestedset

So normally I can build tree-like menu with this code

in controller

$categories= $this->furnituraCategoryRepository->getTree();

in blade

@php

	$traverse = function ($categories) use (&$traverse) {
		echo '<ul class="list-disc ml-4">';
	    foreach ($categories as $category) {
	        echo '<li>'.$category->title;
	        
			if (count($category->children) > 0) {
				$traverse($category->children);
			}

	        echo '</li>';
	    }
	    echo '</ul>';
	};

	$test = $traverse($items);

@endphp

{{ $test }}

But with laravel-menu I cannot catch how to assemble this recursive function.
I tried like this, but it gives me error Value of type null is not callable pointing on $traverse($category->children, $a);

    public function handle(Request $request, Closure $next)
    {

        $categories = $this->CategoryRepository->getTree();

        $traverse = function ($categories, $menu) use (&$traverse) {
            foreach ($categories as $category) {  

                if (count($category->children) > 0) {
                    $menu->group(['prefix' => $category->slug], function($m) use ($category) {
                        $m->add($category->title, '');
                        $m->group(['prefix' => $category->slug], function($a) use (&$traverse, $category) {
                            $traverse($category->children, $a);
                        });
                    });
                }

                $menu->add($category->title, $category->slug);
            };
        };


        Menu::make('Mytopmenu', function($menu) use ($traverse, $furnitura) {
            echo $traverse($furnitura, $menu);
        });

        return $next($request);
    }
@dustingraham
Copy link
Collaborator

This looks like an error with your code, and not so much an error with the library.

Perhaps try removing the recursive requirement and iterating instead, or separating the function declaration out to a class level function, rather than a variable function. I can't help troubleshoot this. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants