欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tp 多表联查 递归

程序员文章站 2024-03-21 13:40:04
...

递归

权限登录的时候调用的
需要保存的数据

 $arr2 = User::node($data['user_name']);
 $data2 = User::getTree($arr2);
 $token = Token::createToken($data['user_id']);
 $data['token'] = $token;
 $data['node'] = $data2;
 \session('node',$data2);
 \session('token',$token);

然后是模型

public static function node($user_name){
	  return self::join('user_role','user.user_id=user_role.u_id')
	       ->join('role','user_role.r_id=role.role_id')
	       ->join('role_node','role.role_id=role_node.r_id')
	       ->join('node','role_node.n_id=node.node_id')
	       ->where('user_name',$user_name)
	       ->select();
}
public static function getTree($data,$pid=0){
	   $list = [];
	   foreach ($data as $v){
	       if ($v['pid'] == $pid){
	           $list[] =$v;
	       }
	   }
	   foreach ($list as $key => $val){
	       $list[$key]['child'] = self::getTree($data,$val['node_id']);
	   }
	   return $list;
}
相关标签: tp php