Category.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\common\models;
  3. use Illuminate\Database\Eloquent\SoftDeletes;
  4. /**
  5. * Created by PhpStorm.
  6. * Author: 芸众商城 www.yunzshop.com
  7. * Date: 2017/2/22
  8. * Time: 下午5:54
  9. */
  10. class Category extends BaseModel
  11. {
  12. use SoftDeletes;
  13. public $table = 'yz_category';
  14. public $attributes = [
  15. 'display_order' => 0,
  16. 'thumb' => '',
  17. 'description' => '',
  18. 'adv_img' => '',
  19. 'adv_url' => '',
  20. ];
  21. /**
  22. * 不可填充字段.
  23. *
  24. * @var array
  25. */
  26. protected $guarded = [''];
  27. //protected $fillable = [''];
  28. /**
  29. * @param $parent_id
  30. * @param $pageSize
  31. * @return mixed
  32. */
  33. public static function getCategorys($parentId)
  34. {
  35. return self::uniacid()
  36. ->where('parent_id', $parentId)
  37. ->orderBy('display_order', 'desc');
  38. }
  39. public static function checkCategory($ids=[]){
  40. if (!$ids) return false;
  41. if(!$category_data = self::uniacid()->whereIn('id',$ids)->orderBy('level','ASC')->get()) return false;
  42. $parent_id = 0;
  43. $category_data->each(function ($v) use (&$parent_id){
  44. if ($v->level != 1 && $v->parent_id != $parent_id){
  45. return false;
  46. }
  47. $parent_id =$v->id;
  48. });
  49. return true;
  50. }
  51. /**
  52. * @param $parentId
  53. * @param $set
  54. * @return mixed
  55. */
  56. public static function getChildrenCategorys($parentId, $set = [])
  57. {
  58. $model = self::uniacid();
  59. if ($set['cat_level'] == 3) {
  60. $model->with(['hasManyChildren'=>function($qurey){
  61. return $qurey->where('enabled', 1)
  62. ->orderBy('display_order', 'desc');
  63. }]);
  64. }
  65. $model->where('parent_id', $parentId);
  66. $model->where('enabled', 1);
  67. $model->orderBy('display_order', 'desc');
  68. $model->orderBy('id', 'asc');
  69. return $model;
  70. }
  71. /**
  72. * @return mixed
  73. */
  74. public static function getRecommentCategoryList()
  75. {
  76. $model = self::uniacid();
  77. return $model;
  78. }
  79. /**
  80. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  81. */
  82. public function hasManyChildren()
  83. {
  84. return $this->hasMany(self::class, "parent_id");
  85. }
  86. /**
  87. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  88. */
  89. public function goodsCategories()
  90. {
  91. return $this->hasMany('app\common\models\GoodsCategory', 'category_id', 'id');
  92. }
  93. public function scopePluginId($query)
  94. {
  95. return $query->where('plugin_id', 0);
  96. }
  97. }