Resource.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Enums\ResponseCodeEnum;
  4. use App\Repositories\Models\Model;
  5. class Resource extends Model
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $table = 'base_resources';
  11. protected $guarded = [];
  12. /**
  13. * The attributes excluded from the model's JSON form.
  14. *
  15. * @var array
  16. */
  17. protected $hidden = [];
  18. /**
  19. * 根据id 获取URL
  20. * @param $id
  21. * @return
  22. */
  23. public static function byIdGetUrl($id)
  24. {
  25. $model = Resource::query()->find($id);
  26. if (!$model) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该资源');
  27. return path_to_url($model->path);
  28. }
  29. public static function byIdGetPath($id)
  30. {
  31. $model = Resource::query()->find($id);
  32. if (!$model) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该资源');
  33. return $model->path;
  34. }
  35. public static function byIdsGetPaths($ids)
  36. {
  37. if (!is_array($ids)) return [];
  38. $models = Resource::query()->whereIn('id', $ids)->select(['id', 'name', 'url', 'original_name'])->get();
  39. return $models->toArray();
  40. }
  41. public function getUrlAttribute($url)
  42. {
  43. return path_to_url($url);
  44. }
  45. }