false, 'name' => 'download_resource', 'autoRelation' => [], 'createTime' => 'create_at', 'updateTime' => 'update_at', 'dateFormat' => 'Y-m-d H:i:s', ]; } /** * 所属分类 */ public function category() { return $this->belongsTo(Category::class, 'cid', 'id'); } /** * 文件大小友好显示(字节 -> KB/MB/GB) */ public function getSizeTextAttr($value, $data) { $size = (int)($data['file_size'] ?? 0); if ($size <= 0) { return '-'; } $units = ['B', 'KB', 'MB', 'GB', 'TB']; $i = 0; while ($size >= 1024 && $i < count($units) - 1) { $size /= 1024; $i++; } return round($size, 2) . ' ' . $units[$i]; } /** * 列表查询作用域:仅正常且已审 */ public function scopeVisible($query) { return $query->where('status', 1)->where('is_audit', 1); } /** * 分类名称(依赖预载入的 category 关联) */ public function getCategoryTitleAttr($value, $data) { if (isset($this->category) && $this->category) { return $this->category->title; } $cid = (int)($data['cid'] ?? 0); if ($cid > 0) { return (new Category())->where('id', $cid)->value('title') ?: '-'; } return '-'; } }