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 .= "<option value='".$item['cid']."'>".$item['cat_name']."</option>";

                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 .= "&nbsp;|";
                $optionsStr .=  "<option value='".$item['cid']."'>".$item['cat_name']."</option>";
                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;
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值