Add multidimensional array as an array item

-1

I'm working with laravel and I'm implementing a menu based on permissions, I'm retrieving menu data from a table in my database. I created a model and in it I'm doing the logic to retrieve the menu items and if the item has a parent_id column with the id of a parent item, then it adds it in the array as a child item, my problem is, mine implementation only wants to add an item, but if the parent item has more than 1 subitem it should create a multidimensional array.  but the output is coming like this:

array:3 [
  0 => array:7 [
    "title" => "boletim"
    "url" => "http://myurl.com.br/aluno/boletim"
    "icon_class" => "fa-bulletin"
    "permission" => "root"
    "sort" => "1"
    "active" => "1"
    "children" => array:1 [
      0 => array:6 [
        "title" => "estatisticas"
        "url" => "http://myurl.com.br/estatisticas"
        "icon_class" => "fa-graph"
        "permission" => "admin"
        "sort" => "1"
        "active" => "1"
      ]
    ]
  ]

output using the laravel's dd () function.

Below is the snippet of code I used:

private function getMenu()
    {
        return Menu::where('user_id', $this->user_id)->get();
    }

    /**
         * @return mixed
         */
        public function builder()
        {
            dd($this->formatMenu($this->getMenu()));
        }


        /**
         * @param array $menu
         *
         * @return array
         */
        public function add(array $menu) {
            return [
                'title' => $menu['display_name'],
                'url' => $menu['url'],
                'icon_class' => $menu['icon_class'],
                'permission' => $menu['permission_name'],
                'sort' => $menu['sort'],
                'active' => $menu['active']
            ];
        }

        /**
         * @param $menu
         *
         * @return array
         */
        public function formatMenu($menu)
        {
            $parentMenu = $menu->where('parent_id', null)->toArray();
            $subMenu = $menu->where('parent_id', '!=', null)->toArray();
            $result = [];

            foreach ($parentMenu as $parent) {
                $id = $parent['id'];
                $menu = $this->add($parent);

                foreach ($subMenu as $sub) {
                    $parentId = intval($sub['parent_id']);

                    if ($parentId === $id) {
                        $menu['children'] = [];
                        array_push($menu['children'], $this->add($sub));
                    }
                }

                array_push($result, $menu);
            }

            return $result;
        }
    
asked by anonymous 11.12.2017 / 18:39

1 answer

1

I found the solution by myself, basically adding a multidimensional array to an item in an array.

I had to add the "children" item before I started my loop and added the child arrays to the parent array.

public function add(array $menu) {
        return [
            'title' => $menu['display_name'],
            'url' => $menu['url'],
            'icon_class' => $menu['icon_class'],
            'permission' => $menu['permission_name'],
            'sort' => $menu['sort'],
            'active' => $menu['active'],
            'children' => []
        ];
    }

public function formatMenu($menu)
    {
        $parentMenu = $menu->where('parent_id', null)->toArray();
        $subMenu = $menu->where('parent_id', '!=', null)->toArray();
        $result = [];

        foreach ($parentMenu as $parent) {
            $id = $parent['id'];
            $menu = $this->add($parent);

            foreach ($subMenu as $sub) {
                $parentId = intval($sub['parent_id']);

                if ($parentId === $id) {
                    array_push($menu['children'], $this->add($sub));
                }
            }

            array_push($result, $menu);
        }

        return collect($result)->toJson();
    }
    
12.12.2017 / 18:59