Category.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Cache;
  5. class Category extends Model
  6. {
  7. protected $table = 'categories';
  8. protected $fillable = ['parent_id','grade','name','slug','icon','status','sort','type','role_id','category_id'];
  9. public static function boot()
  10. {
  11. parent::boot();
  12. /*添加事件监听*/
  13. static::creating(function($category){
  14. });
  15. /*监听删除事件*/
  16. static::deleting(function($category){
  17. $category->questions()->update(['category_id'=>0]);
  18. $category->articles()->update(['category_id'=>0]);
  19. $category->tags()->update(['category_id'=>0]);
  20. $category->experts()->update(['category_id'=>0]);
  21. Category::destroy($category->getSubIds(false));
  22. });
  23. }
  24. /**
  25. * 获取用户问题
  26. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  27. */
  28. public function questions()
  29. {
  30. return $this->hasMany('App\Models\Question','category_id');
  31. }
  32. /**
  33. * 获取用户问题
  34. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  35. */
  36. public function articles()
  37. {
  38. return $this->hasMany('App\Models\Article','category_id');
  39. }
  40. /**
  41. * 获取用户问题
  42. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  43. */
  44. public function tags()
  45. {
  46. return $this->hasMany('App\Models\Tag','category_id');
  47. }
  48. /**
  49. * 获取用户问题
  50. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  51. */
  52. public function experts()
  53. {
  54. return $this->hasMany('App\Models\Authentication','category_id');
  55. }
  56. /**
  57. * 生成select下拉选择框
  58. * @param $categories
  59. * @param int $parentId
  60. * @param int $depth
  61. * @return string
  62. */
  63. public static function makeOptionTree($categories,$selectId=0, $parentId=0, $depth = 0){
  64. $childTree = '';
  65. foreach ($categories as $category) {
  66. if ( $parentId == $category->parent_id ) {
  67. if($category->id == $selectId){
  68. $childTree .= "<option value=\"{$category->id}\" selected>";
  69. }else{
  70. $childTree .= "<option value=\"{$category->id}\">";
  71. }
  72. $depthStr = str_repeat("--", $depth);
  73. $childTree .= $depth ? "&nbsp;&nbsp;|{$depthStr}&nbsp;{$category->name}</option>" : "{$category->name}</option>";
  74. $childTree .= self::makeOptionTree($categories,$selectId, $category->id, $depth + 1);
  75. }
  76. }
  77. return $childTree;
  78. }
  79. public static function getSubCategoryIds($parentId=0){
  80. $ids = '';
  81. $categories = self::loadFromCache('all');
  82. foreach ($categories as $category) {
  83. if ( $parentId == $category->parent_id ) {
  84. $ids .= $category->id.' ';
  85. $ids .= self::getSubCategoryIds($category->id);
  86. }
  87. }
  88. return $ids;
  89. }
  90. /*获取分类子分类ID*/
  91. public function getSubIds($wrap=true){
  92. $ids = self::getSubCategoryIds($this->id);
  93. $subIds = trim($ids);
  94. if($wrap){
  95. $subIds = $this->id.' '.$subIds;
  96. }
  97. $subCategoryIds = explode(" ",$subIds);
  98. $subCategoryIds =array_filter($subCategoryIds);
  99. sort($subCategoryIds);
  100. return $subCategoryIds;
  101. }
  102. public static function loadFromCache($type='all'){
  103. $globalCategories = Cache::rememberForever('global_all_categories',function() {
  104. return self::where('status','>',0)->orderBy('sort','asc')->orderBy('created_at','asc')->get();
  105. });
  106. /*返回所有分类*/
  107. if($type == 'all'){
  108. return $globalCategories;
  109. }
  110. /*按类文档型返回分类*/
  111. $categories = [];
  112. foreach( $globalCategories as $category ){
  113. if( str_contains($category->type,$type) ){
  114. $categories[] = $category;
  115. }
  116. }
  117. return $categories;
  118. }
  119. /*查找摸一个分类信息*/
  120. public static function findFromCache($categoryId){
  121. $categories = self::loadFromCache('all');
  122. foreach($categories as $category){
  123. if($categoryId == $category->id){
  124. return $category;
  125. }
  126. }
  127. return false;
  128. }
  129. /**生成树状结构
  130. * @param $categories
  131. * @param int $parentId
  132. * @return array
  133. */
  134. public static function makeTree($categories,$parentId=0){
  135. $tree = [];
  136. foreach ($categories as $category) {
  137. if ( $parentId == $category->parent_id ) {
  138. $category->_child = self::makeTree($categories,$category->id);
  139. $tree[] = $category;
  140. }
  141. }
  142. return $tree;
  143. }
  144. /**
  145. * 获取参数的所有父级分类
  146. * @param int $cid 分类id
  147. * @return array 参数分类和父类的信息集合
  148. * @author huajie <banhuajie@163.com>
  149. */
  150. public static function getParentCategories($categoryId){
  151. if(!$categoryId){
  152. return [];
  153. }
  154. $allCategories = self::loadFromCache('all');
  155. $category = self::findFromCache($categoryId);
  156. $parentId = $category->parent_id;
  157. $parentCategories[] = $category;
  158. while(true){
  159. foreach ($allCategories as $key => $value){
  160. if($value->id == $parentId){
  161. $parentId = $value->parent_id;
  162. array_unshift($parentCategories, $value); //将父分类插入到数组第一个元素前
  163. }
  164. }
  165. if($parentId == 0){
  166. break;
  167. }
  168. }
  169. return $parentCategories;
  170. }
  171. /*判断是否有子分类*/
  172. public function hasChild(){
  173. $categories = self::loadFromCache('all');
  174. foreach($categories as $category){
  175. if($category->parent_id == $this->id){
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. /*获取分类下的子分类*/
  182. public function getSubChild(){
  183. $categories = self::loadFromCache('all');
  184. $children = [];
  185. foreach($categories as $category){
  186. if($category->parent_id == $this->id){
  187. $children[] = $category;
  188. }
  189. }
  190. return $children;
  191. }
  192. }