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

82 lines
1.8 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 getInfo($id,$field = '*')
  33. {
  34. return self::select($field)->find($id);
  35. }
  36. /**
  37. * 获取二级分类数组
  38. * id为键,title为值
  39. * @return array
  40. */
  41. public static function getArray($where = [],$options = [])
  42. {
  43. $model = self::where('status',1)
  44. ->whereNull('deleted_at');
  45. if(count($where) > 0){
  46. $model->where($where);
  47. }
  48. $list = $model->pluck('id','title')->toArray();
  49. if(!empty($options)){
  50. $new = array_merge($options,$list);
  51. return array_flip($new);
  52. }else{
  53. return array_flip($list);
  54. }
  55. }
  56. /**
  57. * 处理旧图片
  58. * @param $value
  59. * @return string
  60. */
  61. public function imageUrl($value)
  62. {
  63. if(strripos($value,"http") === false){
  64. return config('filesystems.disks.oss.img_host').'/'.$value;
  65. }else{
  66. return $value;
  67. }
  68. }
  69. }