2025-11-21 17:26:43 +03:00

60 lines
2.4 KiB
PHP

<?php
namespace App\Http\Resources\Intervention;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\Deceased\DeceasedResource;
use App\Http\Resources\Client\ClientResource;
use App\Http\Resources\Employee\ThanatopractitionerResource;
class InterventionResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'client' => $this->whenLoaded('client', function () {
return new ClientResource($this->client);
}),
'deceased' => $this->whenLoaded('deceased', function () {
return new DeceasedResource($this->deceased);
}),
'order_giver' => $this->order_giver,
'location' => $this->whenLoaded('location', function () {
return [
'id' => $this->location->id,
'name' => $this->location->name
];
}),
'type' => $this->type,
'scheduled_at' => $this->scheduled_at ? $this->scheduled_at->format('Y-m-d H:i:s') : null,
'duration_min' => $this->duration_min,
'status' => $this->status,
'practitioners' => $this->whenLoaded('practitioners', function () {
return ThanatopractitionerResource::collection($this->practitioners);
}),
'principal_practitioner' => $this->whenLoaded('practitioners', function () {
$principal = $this->practitioners->where('pivot.role', 'principal')->first();
return $principal ? new ThanatopractitionerResource($principal) : null;
}),
'attachments_count' => $this->attachments_count,
'notes' => $this->notes,
'created_by' => $this->created_by,
'created_at' => $this->created_at->format('Y-m-d H:i:s'),
'updated_at' => $this->updated_at->format('Y-m-d H:i:s'),
'attachments' => $this->whenLoaded('attachments', function () {
return InterventionAttachmentResource::collection($this->attachments);
}),
'notifications' => $this->whenLoaded('notifications', function () {
return InterventionNotificationResource::collection($this->notifications);
})
];
}
}