NotificationResource.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Http\Resources;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. use Illuminate\Support\Facades\Storage;
  7. class NotificationResource extends JsonResource
  8. {
  9. public function toArray(Request $request): array
  10. {
  11. return [
  12. 'id' => $this->id,
  13. 'type' => $this->type,
  14. 'title' => $this->title,
  15. 'message' => $this->message,
  16. 'recipient' => $this->recipient,
  17. 'recipient_position_id' => $this->recipient_position_id,
  18. 'recipient_sector_id' => $this->recipient_sector_id,
  19. 'sent_count' => $this->when($this->sends_count !== null, $this->sends_count),
  20. 'seen_count' => $this->when($this->seen_count !== null, $this->seen_count),
  21. 'image_url' => $this->whenLoaded('media', function () {
  22. $path = $this->media->first()?->path;
  23. return $path ? Storage::disk('s3')->temporaryUrl($path, now()->addHours(24)) : null;
  24. }),
  25. 'created_by' => $this->created_by,
  26. 'created_by_user' => $this->whenLoaded('createdBy', fn() => new UserResource($this->createdBy)),
  27. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  28. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  29. ];
  30. }
  31. }