NotificationResource.php 1.3 KB

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