*/ 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); }) ]; } }