0

I have trouble fixing this problem. Error is

Undefined variable: subMenus (View: C:\xampp\htdocs\packages\astral\resources\views\admin.blade.php)

Here is the code

public function adminMenus()
{
    $adminMenus = $this->all(true);
    
    $result = $adminMenus->map(function ($item, $index) {
        $routeName = $item->route();
        if ($item->hasSubMenu()) {
            $subMenus = collect($item->subMenu)->map(function ($item) {
                $routeName = $item->route();

                return [
                    'name' => $item->label(),
                    'url' => $routeName === '#' ? '#' : route($routeName, $item->params()),
                ];
            });
        }

        return [
            'name' => $item->label(),
            'icon' => $item->icon(),
            'url' => $routeName === '#' ? '#' : route($routeName, $item->params()),
            'submenus' => $subMenus,
        ];
    });

    return $result;
}
ajbm6
  • 1
  • `$subMenus` only exists if it has submenus. If it doesn't, the variable doesn't exist and you get that error. – aynber Sep 01 '22 at 17:17
  • Can you please explain? I'm still new to laravel so many things doesn't make sense to me yet. Any possible solution? – ajbm6 Sep 01 '22 at 17:23
  • Read the linked duplicate, but the easiest is to define `$subMenus` with a dummy/default value before you go into `$item->hasSubMenu` – aynber Sep 01 '22 at 17:26

0 Answers0