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

61 lines
1.4 KiB
PHP

<?php
namespace App\Models;
use App\Casts\StorageFile;
use App\Enums\Gender;
use App\Traits\HasDateTimeFormatter;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Foundation\Auth\User as Authenticate;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticate
{
use HasApiTokens, Filterable, HasDateTimeFormatter;
protected $fillable = ['address', 'birthday', 'name', 'password', 'phone', 'sex', 'avatar'];
protected $appends = ['age', 'sex_text', 'birthday_format'];
protected $hidden = ['password'];
protected $casts = [
'sex' => Gender::class,
'avatar' => StorageFile::class,
'birthday' => 'date',
'password' => 'hashed',
];
public function patients()
{
return $this->hasMany(Patient::class, 'user_id');
}
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 birthdayFormat(): Attribute
{
return new Attribute(
get: fn () => $this->birthday ? $this->birthday->format('Y-m-d') : '',
);
}
public function scopeSort($q)
{
return $q->orderBy('created_at', 'desc');
}
}