viernes, 4 de noviembre de 2022

Cómo colocar el child en la categoría de usuario de subcategoría y subcategoría en JSON Laravel

$parents = Category::where('parent_id', 0)->where('status', 1)->orderBy('sort_order', 'asc')->get();
        foreach ($parents as $parent) {
            $childs = Category::where('parent_id', $parent->id)->where('status', 1)->orderBy('sort_order', 'asc')->get();
            if (count($childs) > 0) {
                $subCat = array();
                $players = array();
                $roster[$parent->name] = $players;
                        foreach ($childs as $i => $child) {
                            $subchilds = Category::where('parent_id', $child->id)->where('status', 1)->orderBy('sort_order', 'asc')->get();
                            if (count($subchilds) > 0) {

                                $roster[$parent->name][$child->name] = $subCat;
                                foreach ($subchilds as $subchild) {

                                    $roster[$parent->name][$child->name][$subchild->id] = $subchild->name;
                                }

                            }else{
                                $roster[$parent->name][$child->name] = $players;
                            }
                        }

            }
        }
        return $roster;

Fuente https://stackoverflow.com/questions/59577909/how-to-get-child-under-sub-category-and-sub-category-user-category-in-json-larav

No hay comentarios:

Publicar un comentario

Blogs Colombia