1
0
Fork 0
medical-record-server/app/Models/Patient.php

92 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Gender;
use EloquentFilter\Filterable;
use App\Traits\HasDateTimeFormatter;
use Slowlyo\OwlAdmin\Models\AdminUser;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* 病人
*/
class Patient extends Model
{
use HasDateTimeFormatter, Filterable;
protected $fillable = [
'name', 'sex', 'phone', 'address', 'birthday',
'treat_at', 'illness', 'remarks', 'images',
'doctor_id', 'inviter_id', 'saler_id', 'type_id',
];
protected $appends = ['age', 'sex_text', 'treat_format', 'birthday_format'];
protected $casts = [
'sex' => Gender::class,
'treat_at' => 'date',
'birthday' => 'date',
'images' => 'json',
];
protected function age(): Attribute
{
return new Attribute(
get: fn () => $this->birthday ? $this->birthday->diffInYears() : '',
);
}
protected function sexText(): Attribute
{
return new Attribute(
get: fn () => $this->sex ? $this->sex->text() : '',
);
}
protected function treatFormat(): Attribute
{
return new Attribute(
get: fn () => $this->treat_at ? $this->treat_at->format('Y-m-d') : '',
);
}
protected function birthdayFormat(): Attribute
{
return new Attribute(
get: fn () => $this->birthday ? $this->birthday->format('Y-m-d') : '',
);
}
public function doctor()
{
return $this->belongsTo(AdminUser::class, 'doctor_id');
}
public function inviter()
{
return $this->belongsTo(AdminUser::class, 'inviter_id');
}
public function saler()
{
return $this->belongsTo(AdminUser::class, 'saler_id');
}
public function type()
{
return $this->belongsTo(Keyword::class, 'type_id');
}
public function records()
{
return $this->hasMany(PatientRecord::class, 'patient_id');
}
public function scopeSort($q)
{
return $q->orderBy('id', 'desc');
}
}