链街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.

72 lines
1.7 KiB

  1. <?php
  2. namespace App\Models;
  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. * id为键,name为值
  29. * @return array
  30. */
  31. public static function getCategoryArray($where = [],$options = [])
  32. {
  33. $model = self::select('id','title')
  34. ->where('status',1)
  35. ->whereNull('deleted_at');
  36. if(count($where) > 0){
  37. $model->where($where);
  38. }
  39. $list = $model->get();
  40. $array = empty($options) ? [] : $options;
  41. if(count($list) > 0){
  42. foreach ($list as $value) {
  43. $array[$value->id] = $value->title;
  44. }
  45. }
  46. return $array;
  47. }
  48. /**
  49. * 处理旧图片
  50. * @param $value
  51. * @return string
  52. */
  53. public function imageUrl($value)
  54. {
  55. if(strripos($value,"http") === false){
  56. return env('OSS_IMG_HOST').'/'.$value;
  57. }else{
  58. return $value;
  59. }
  60. }
  61. }