Erp系统常用递归,查类目树,查上级,查下级
1、无限极往上获取平台类目树信息
数据结构:商品类目id《category_id,商品类目父id《parent_id
数据需求:根据传入最低层类目id,获取所有上级类目信息(包含自己)
代码如下:
1 // 无限极往上获取平台类目树信息 2 public function platformCategoryVerify($platform, $site_code, $platform_category_id, $tree) 3 { 4 $apCategories_info = ApCategories::where([["category_id" , $platform_category_id],["platform" , $platform],["site_code" , $site_code]])->first(["category_id", "category_name", "parent_id", "level"]); 5 if(!empty($apCategories_info->category_id)){ 6 $tree[] = $apCategories_info->toArray(); 7 $tree = $this->platformCategoryVerify($platform, $site_code, $apCategories_info->parent_id, $tree); //此处加“$tree = ” 是递归的关键,不然会导致 $tree数组,被覆盖 8 } 9 return $tree; 10 }