lavary/laravel-menu

FindOrCreate

lazosweb opened this issue · 3 comments

On my web application I have modules. Each module has Middleware that creates a part of the Menu.
1st Module has this Middleware

Menu::make('A', function ($menu) {
});

2nd Module has this Middleware

Menu::make('A', function ($menu) {
                $menu->add('B');
                    $menu->b->add('C');
                }
        });

3nd Module has this Middleware

Menu::make('A', function ($menu) {
                $menu->add('B');
                    $menu->b->add('D');
                }
        });

Lavary already knows that the Menu A exists and adds all the items under the same object but this does not work with the items.
The problem I am having is that on my web application if the 2nd and 3rd module are both present it will add the item B twice. I want it to check if it exists and get the object or create it in case only one of the (2nd, 3rd) modules are presented.

Is this somehow possible?

Perhaps check if it exists before adding.

Menu::make('A', function ($menu) {
    if (!$menu->b) {
        $menu->add('B');
    }
    $menu->b->add('D');
});

That's what I did, I was wondering if there is a more proper way of doing it... If not, then my question was answered.

Not that I know of right now.