1. 程式人生 > >laravel 學習之文章分類

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; } }