*/ public function toArray(Request $request): array { return [ 'id' => $this->MedicalHistoryID, 'num' => $this->num, 'mkb' => $this->whenLoaded('migrations', function () { return [ 'ds' => $this->migrations()->first()->diagnosis()->first()?->mkb()->first()->DS ?? null, 'name' => $this->migrations()->first()->diagnosis()->first()?->mkb()->first()->NAME ?? null, ]; }), 'operations' => $this->whenLoaded('surgicalOperations', function () { return $this->surgicalOperations()->where('rf_StationarBranchID', $this->misStationarBranchId) ->get() ->map(function (MisSurgicalOperation $operation) { return [ 'code' => $operation->serviceMedical->ServiceMedicalCode ?? null, ]; }); }), 'fullname' => Str::ucwords(Str::lower("$this->FAMILY $this->Name $this->OT")), 'age' => Carbon::parse($this->BD)->diff(Carbon::now())->format('%y'), 'birth_date' => Carbon::parse($this->BD)->format('d.m.Y'), ]; } }