purityselect/app/Models/PatientNote.php
2024-10-25 01:05:27 +05:00

115 lines
3.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Auth;
class PatientNote extends Model
{
protected $table = 'patient_notes';
protected $fillable = [
'note',
'note_type',
'patient_id',
'patient_notes',
'appointment_id',
'telemed_pros_id',
'created_by_id',
'created_by_type'
];
protected $appends = ['created_by'];
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (Auth::guard('admin')->check()) {
$model->created_by_id = Auth::guard('admin')->id();
$model->created_by_type = 'App\Models\Admin';
} elseif (Auth::check()) {
$model->created_by_id = Auth::id();
$model->created_by_type = 'App\Models\Telemedpro';
}
});
}
public function getCreatedByAttribute()
{
if (!$this->created_by_type || !$this->created_by_id) {
return null;
}
$model = $this->created_by_type::find($this->created_by_id);
if (!$model) {
return null;
}
$type = $this->created_by_type === 'App\Models\Admin' ? 'Admin' : 'Agent';
return $model->name . ' (' . $type . ')';
}
/* public function getCreatedByAttribute()
{
// Check if we're using a custom select
if (!array_key_exists('created_by_id', $this->attributes) || !array_key_exists('created_by_type', $this->attributes)) {
return $this->provide_name ? $this->provide_name . ' (Provider)' : null;
}
if (!$this->created_by_type || !$this->created_by_id) {
return $this->provide_name ? $this->provide_name . ' (Provider)' : null;
}
$model = $this->created_by_type::find($this->created_by_id);
if (!$model) {
return $this->provide_name ? $this->provide_name . ' (Provider)' : null;
}
$type = match ($this->created_by_type) {
'App\Models\Admin' => 'Admin',
'App\Models\Telemedpro' => 'TelemedPro',
//default => 'User',
};
return $model->name . ' (' . $type . ')';
} */
protected $hidden = ['created_by_id', 'created_by_type'];
public function createdBy(): MorphTo
{
return $this->morphTo();
}
public function getCreatedByNameAttribute()
{
return $this->createdBy ? $this->createdBy->name : null;
}
public function appointment()
{
return $this->belongsTo(Appointment::class, 'appointment_id', 'id');
}
public function telemedPro()
{
return $this->belongsTo(TelemedPro::class, 'telemed_pros_id', 'id')->withDefault();
}
public function admin()
{
return $this->belongsTo(Admin::class, 'admin_id', 'id')->withDefault();
}
public function patient()
{
return $this->belongsTo(Patient::class, 'patient_id', 'id');
}
}