attributes['allergy'] = '-' . arr2str($val, '-') . '-'; } public function getAllergyAttribute($val) { return str2arr(trim($val, '-'), '-'); } public function setPastMedicalAttribute($val) { if (count($val) == 0) return ''; $this->attributes['past_medical'] = '-' . arr2str($val, '-') . '-'; } public function getPastMedicalAttribute($val) { return str2arr(trim($val, '-'), '-'); } // public function records() // { // return $this->hasMany(Record::class)->where('status', Record::STATUS_OVER); // } // public function prescriptions() { return $this->hasMany(Prescription::class); } public function medicals() { return $this->hasMany(MedicalRecord::class); } public function getAgeAttribute() { if (is_null($this->attributes['birth_date'])) return '--'; return Carbon::parse($this->attributes['birth_date'])->age; } public function getSexTextAttribute() { return SexEnum::getDescription($this->attributes['sex']); } public function getSourceTextAttribute() { return Category::byIdGetCategoryValue($this->attributes['source']); } public function getAllergyTextAttribute() { if (!$this->attributes['allergy']) return ''; $ids = str2arr(trim($this->attributes['allergy'], '-'), '-'); return Category::byIdsGetCategoryValue($ids); } public function getPastMedicalTextAttribute() { if (!$this->attributes['past_medical']) return ''; $ids = str2arr(trim($this->attributes['past_medical'], '-'), '-'); return Category::byIdsGetCategoryValue($ids); } public function getLatelyRecordAttribute() { $record = self::medicals()->orderByDesc('id')->first(); if ($record) { return [ 'doctor' => $record->doctor, 'date' => $record->created_at->format('Y-m-d') ]; } else { return [ 'doctor' => str_is_null(), 'date' => str_is_null() ]; } } public function getVisitCountAttribute() { return $this->medicals()->where('status', ModelStatusEnum::OK)->count(); } }