使用模型,循环遍历报错,如何实现?

浏览:279 发布日期:2018/08/26 分类:ThinkPHP5专区 关键字: TP5.1模型遍历
以下是model下menu方法$count = User::count();
        if($count > 0 ) {
            $res = LeftMenu::where('level',1)->select();
            $data="";
            foreach($res as $key => $val){
                          //2级
                    $res2 = LeftMenu::where('pid',$val['id'])->order('id', 'asc')->select();
                    $data2="";
                    foreach($res2 as $k2 => $v2){
                                      //3级
                                $res3 = LeftMenu::where('pid',$v2['id'])->order('id', 'asc')->select();
                                $data3="";
                                foreach($res3 as $k3 => $v3){
                                  $data3[$k3]['title']=$v3['title'];
                                  $data3[$k3]['name']=$v3['name'];
                                  $data3[$k3]['icon']=$v3['icon'];
                                  $data3[$k3]['jump']=$v3['jump'];
                                  $data3[$k3]['title']=$v3['title'];
                                  $data3[$k3]['status']=$v3['status'];
                                  $data3[$k3]['rule']=$v3['rule'];
                                  $data3[$k3]['param']=$v3['param'];     
                                  // dump($res3);die;
                                }
                      $data2[$k]['title']=$v2['title'];
                      $data2[$k]['name']=$v2['name'];
                      $data2[$k]['spread']= true;
                      $data2[$k]['icon']=$v2['icon'];
                      $data2[$k]['jump']=$v2['jump'];
                      $data2[$k]['status']=$v2['status'];
                      $data2[$k]['rule']=$v2['rule'];
                      $data2[$k]['param']=$v2['param'];
                      $data2[$k]['list']=$data3;   
                      // dump($res2);die;           
                    }
              $data['name']=$val['name'];
              $data['title']=$val['title'];
              $data['icon']=$val['icon'];
              $data['jump']=$val['jump'];
              $data['spread']= true;
              $data['status']=$val['status'];
              $data['rule']=$val['rule'];
              $data['param']=$val['param'];
              $data['list']=$data2;
              dump($count);dump($val);dump($key);die;
            }
            return $data;
        }
以下是controller下menu方法        $Leftmenus = new Leftmenumodel; 
        $res = $Leftmenus->menu();
        if(!$res){
            $this->return_msg(400,'系统错误,请重试!');
        }else{
            $this->return_msg(200,'',$res);
        }
数据库结构


想要实现的api结构


报错1


该如何修改,才能得到想要的API数据结构?求大神指导。
最佳答案
评论( 相关
后面还有条评论,点击查看>>