laravel 学习之文章分类

模型


/*
 * 获取分类列表
 * @is_show : 是否显示,默认显示全部 0:表示读取不显示的, 1:表示读取显示的
 */
public function getCategoryList($is_show = ""){

    if(!empty($is_show)){
        $condition['category.is_show'] = $is_show;
        $res = $this->model->leftJoin("module", "module.m_id", "=", "category.m_id")->where($condition)->orderBy("sort")->get();

    }else{
        $res = $this->model->leftJoin("module", "module.m_id", "=", "category.m_id")->orderBy("sort")->get();

    }

    return $res;
}


控制器


$cat = $this->model->getCategoryList();
//var_dump($cat);
$tree = new CategoryTree($cat);
$category = $tree->getList();


CategoryTree类

namespace App\Libraries;
class CategoryTree{

    private $tree = [];
    private $list = [];
    private $options;
    private $category;

    public function __construct($category)
    {
        $this->category = $category;
    }

    /*
     * 获取分类树型数组
     */
    public function getTreeList(){
        $parent = $this->category->where('parent_id', 0);
        if(count($parent) > 0){
            foreach($parent as $item) {
                $this->tree[$item['cid']]['cid'] = $item['cid'];
                $this->tree[$item['cid']]['module_name'] = trans("common.".$item['m_module']);
                $this->tree[$item['cid']]['cat_name'] = $item['cat_name'];
                $this->tree[$item['cid']]['cat_ename'] = $item['cat_ename'];
                $this->tree[$item['cid']]['parent_id'] = $item['parent_id'];
                $this->tree[$item['cid']]['depth'] = $item['depth'];
                $this->tree[$item['cid']]['cat_path'] = $item['cat_path'];
                $this->tree[$item['cid']]['path'] = $item['path'];
                $this->tree[$item['cid']]['m_id'] = $item['m_id'];
                $this->tree[$item['cid']]['is_show'] = $item['is_show'];
                $this->tree[$item['cid']]['keywords'] = $item['keywords'];
                $this->tree[$item['cid']]['description'] = $item['description'];
                $this->tree[$item['cid']]['sort'] = $item['sort'];
                $this->tree[$item['cid']]['style'] = "";
                if($item['cid']){
                    $this->tree[$item['cid']]['children'] = $this->_getChildren($item['cid']);
                }
            }
        }
        return $this->tree;
    }

    /*
     * 递归获取子分类
     */
    protected function _getChildren($cid){
        $children = $this->category->where("parent_id", $cid);
        $treeArr = [];
        if(count($children) > 0){
            foreach($children as $item)  {

                $treeArr[$item['cid']]['cid'] = $item['cid'];
                $treeArr[$item['cid']]['module_name'] = trans("common.".$item['m_module']);
                $treeArr[$item['cid']]['cat_name'] = $item['cat_name'];
                $treeArr[$item['cid']]['cat_ename'] = $item['cat_ename'];
                $treeArr[$item['cid']]['parent_id'] = $item['parent_id'];
                $treeArr[$item['cid']]['depth'] = $item['depth'];
                $treeArr[$item['cid']]['cat_path'] = $item['cat_path'];
                $treeArr[$item['cid']]['path'] = $item['path'];
                $treeArr[$item['cid']]['m_id'] = $item['m_id'];
                $treeArr[$item['cid']]['is_show'] = $item['is_show'];
                $treeArr[$item['cid']]['keywords'] = $item['keywords'];
                $treeArr[$item['cid']]['description'] = $item['description'];
                $treeArr[$item['cid']]['sort'] = $item['sort'];
                if($item['depth'] == 2){
                    $this->tree[$item['cid']]['style'] = "";
                }else{
                    $this->tree[$item['cid']]['style'] = "";
                }
                if($item['cid']){
                   $treeArr[$item['cid']]['children'] = $this->_getChildren($item['cid']);
                }
            }

        }
        return $treeArr;
    }

    /*
     * 获取options列表
     */
    public function getOptionsList(){
        $parent = $this->category->where('parent_id', 0);
        if(count($parent) > 0){
            foreach($parent as $item) {
                $this->options .= "]."'>".$item['cat_name']."";

                if($item['cid']){
                    $this->options .= $this->_getOptionsChildren($item['cid']);
                }
            }
        }

        return $this->options;
    }
    /*
     * 递归获取子分类
     */
    protected function _getOptionsChildren($cid){
        $children = $this->category->where("parent_id", $cid);
        $optionsStr = "";
        if(count($children) > 0){
            foreach($children as $item)  {
                $optionsStr .= " |";
                $optionsStr .=  "]."'>".$item['cat_name']."";
                if($item['cid']){
                    $optionsStr .= $this->_getOptionsChildren($item['cid']);
                }
            }

        }
        return $optionsStr;
    }

    /**
     * 获取当前id的子ID
     * @param int $layer 当前层级
     */
    public function getList($pid = 0, $pname="", $depth=0)
    {

        $this->list    = empty($depth) ? array() : $this->list;
        if(empty($pname)){
            $pname = trans('common.top_cat');
        }
        foreach($this->category as $k => $row)
        {
            /**
             * 如果父ID为当前传入的id
             */
            if($row['parent_id'] == $pid)
            {
                //如果当前遍历的id不为空
                $row['parent_name'] = $pname;
                $row['module_name'] = trans("common.".$row['m_module']);
                $this->list[]    = $row;
                //var_array($arr);
                $this->getList($row['cid'], $row['cat_name'], $row['depth']+1);//递归调用
            }
        }
        return $this->list;
    }


}

你可能感兴趣的:(php)