| 123456789101112131415161718192021222324252627282930313233 |
- <?php
- namespace App\Http\Resources;
- use Illuminate\Http\Request;
- use Illuminate\Http\Resources\Json\JsonResource;
- class AttachmentResource extends JsonResource
- {
- public function toArray(Request $request): array
- {
- return [
- 'id' => $this->id,
- 'filename' => $this->filename,
- 'filepath' => $this->filepath,
- 'mime_type' => $this->mime_type,
- 'size' => $this->size,
- 'size_human' => $this->formatBytes($this->size),
- 'created_at' => $this->created_at->toISOString(),
- ];
- }
- private function formatBytes($bytes, $precision = 2)
- {
- $units = ['B', 'KB', 'MB', 'GB', 'TB'];
-
- for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
- $bytes /= 1024;
- }
-
- return round($bytes, $precision) . ' ' . $units[$i];
- }
- }
|