链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.9 KiB

  1. <?php
  2. namespace App\Models\v3;
  3. use Illuminate\Database\Eloquent\SoftDeletes;
  4. use Dcat\Admin\Traits\ModelTree;
  5. use Illuminate\Database\Eloquent\Model;
  6. class Category extends Model
  7. {
  8. use ModelTree;
  9. use SoftDeletes;
  10. protected $table = 'lanzu_category';
  11. protected $dateFormat = 'U';
  12. // 父级ID字段名称,默认值为 parent_id
  13. // protected $parentColumn = 'pid';
  14. // 排序字段名称,默认值为 order
  15. protected $orderColumn = 'sort';
  16. // 标题字段名称,默认值为 title
  17. // protected $titleColumn = 'name';
  18. protected $appends = [
  19. 'cover_img_url',
  20. ];
  21. public function getCoverImgUrlAttribute($value)
  22. {
  23. $value = $value ? $value : $this->cover_img;
  24. return $this->imageUrl($value);
  25. }
  26. /**
  27. * 获取单个信息
  28. * @param int $id
  29. * @param string $field
  30. * @return string
  31. */
  32. public static function getCategoryInfo($id,$field = '*')
  33. {
  34. return self::select($field)->find($id);
  35. }
  36. /**
  37. * 获取二级分类数组
  38. * id为键,name为值
  39. * @return array
  40. */
  41. public static function getCategoryArray($where = [],$options = [])
  42. {
  43. $model = self::select('id','title')
  44. ->where('status',1)
  45. ->whereNull('deleted_at');
  46. if(count($where) > 0){
  47. $model->where($where);
  48. }
  49. $list = $model->get();
  50. $array = empty($options) ? [] : $options;
  51. if(count($list) > 0){
  52. foreach ($list as $value) {
  53. $array[$value->id] = $value->title;
  54. }
  55. }
  56. return $array;
  57. }
  58. /**
  59. * 处理旧图片
  60. * @param $value
  61. * @return string
  62. */
  63. public function imageUrl($value)
  64. {
  65. if(strripos($value,"http") === false){
  66. return env('OSS_IMG_HOST').'/'.$value;
  67. }else{
  68. return $value;
  69. }
  70. }
  71. }