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 createdBy(): MorphTo { return $this->morphTo(); } 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 patient() { return $this->belongsTo(Patient::class, 'patient_id'); } public function prescription() { return $this->belongsTo(Prescription::class, 'prescription_id'); } public function appointment() { return $this->belongsTo(Appointment::class, 'appointment_id', 'id'); } }