123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace App\Repositories\Models\Base;
- use App\Repositories\Enums\ResponseCodeEnum;
- use App\Repositories\Models\Model;
- class Resource extends Model
- {
- /**
- * @var string
- */
- protected $table = 'base_resources';
- protected $guarded = [];
- /**
- * The attributes excluded from the model's JSON form.
- *
- * @var array
- */
- protected $hidden = [];
- /**
- * 根据id 获取URL
- * @param $id
- * @return
- */
- public static function byIdGetUrl($id)
- {
- $model = Resource::query()->find($id);
- if (!$model) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该资源');
- return path_to_url($model->path);
- }
- public static function byIdGetPath($id)
- {
- $model = Resource::query()->find($id);
- if (!$model) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该资源');
- return $model->path;
- }
- public static function byIdsGetPaths($ids)
- {
- if (!is_array($ids)) return [];
- $models = Resource::query()->whereIn('id', $ids)->select(['id', 'name', 'url', 'original_name'])->get();
- return $models->toArray();
- }
- public function getUrlAttribute($url)
- {
- return path_to_url($url);
- }
- }
|