如何在 JSON Laravel 中获取子类别和子类别用户类别下的孩子

这是一个非常常见的问题,但我无法以 JSON 格式为 API 制作它。我是一个表类别,其中存储了三层类别。类别->子类别->子项。我想在 API 格式中得到这个。


就像是。


Category


  sub-category-1

  sub-category-2 

      child-2-1

      child-2-2

  sub-category-3

我尝试使用代码


  $allData = array();

            // get all parent category 

            $categories = Category::where(['status' => 1, 'parent_id' => 0])->get();        

            foreach ($categories as $key=>$sub) {

                    // now take one by one it's child category 

                    $allData[$key]['parent'] = $sub->name;

                    $subCategory = Category::where('status', 1)->where('parent_id', '=', $sub->id)->get();

                    $subCat = array();

                    // set parent category title

                    foreach ($subCategory as $k=>$subcat) {

                        $subCat[$subcat->id] = $subcat->name;


                    }

                    // set subcategory array

                    $allData[$key]['subcategory'] = $subCat;

            }

           return $allData;

我得到了这个结果不是我不知道如何获得孩子的价值

http://img3.mukewang.com/62d21e76000177bc04220402.jpg

数据库看起来像


http://img1.mukewang.com/62d21e830001ef9502770474.jpg

拉莫斯之舞
浏览 90回答 2
2回答

撒科打诨

最后,我解决了这个       $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;

喵喵时光机

这是你如何做到的。&nbsp;$allData = array();&nbsp; &nbsp; &nbsp; &nbsp; // get all parent category&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $categories = Category::where(['status' => 1, 'parent_id' => 0])->get();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($categories as $key=>$sub) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // now take one by one it's child category&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $allData[$key]['parent'] = $sub->name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $subCategory = Category::where('status', 1)->where('parent_id', '=', $sub->id)->get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $subCat = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set parent category title&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($subCategory as $k=>$subcat) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $subCat[$subcat->id] = $subcat->name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //children of subcat&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $children = Category::where('status', 1)->where('parent_id', '=', $subcat->id)->get();&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $child = array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($children){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($children as $k1=>$val){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$child[$val->id] = $val->name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$allData[$key]['subcategory'][$subcat->id]['child'] = $child;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set subcategory array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $allData[$key]['subcategory'] = $subCat;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;return $allData;请尝试这种方式。将您的 json 转换为数组并使用它来获取子值$ar = json_decode($data,true);foreach($ar as $value){&nbsp; &nbsp; echo $value['parent'].'<br>';&nbsp; &nbsp; foreach($value['child'] as $child){&nbsp; &nbsp; &nbsp; &nbsp;echo '&nbsp;'.$child.'<br>';&nbsp; &nbsp;}&nbsp;}这将输出这个test&nbsp;A&nbsp;B&nbsp;C&nbsp;D&nbsp;Etest1&nbsp;G&nbsp;H&nbsp;I&nbsp;J&nbsp;K
打开App,查看更多内容
随时随地看视频慕课网APP