From c9bd6664f2e73628a2a6368ce9bac021ebe01607 Mon Sep 17 00:00:00 2001 From: rahmatrafli1 Date: Fri, 3 Oct 2025 09:07:42 +0700 Subject: [PATCH] refactor(usermanagement): tidy up User model code structure and improve attribute casting --- app/Models/User.php | 177 +++++++++++++++++++++++--------------------- 1 file changed, 94 insertions(+), 83 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 97d7034..2d74b23 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -1,94 +1,105 @@ */ - class User extends Authenticatable + protected $fillable = [ + 'name', + 'email', + 'password', + 'nik', + 'branch_id', + 'profile_photo_path', + 'last_login_at', + 'last_login_ip', + 'sign' + ]; + + /** + * The attributes that should be hidden for serialization. + * + * These are the attributes that will be hidden when the model is converted to an array or JSON. + * + * @var array + */ + protected $hidden = [ + 'password', + 'remember_token', + ]; + + /** + * Get the attributes that should be cast. + * + * This method defines how the attributes should be cast when accessed. + * In this case, 'email_verified_at' is cast to 'datetime', 'password' is cast to 'hashed', and 'id' is cast to 'string'. + * + * @return array + */ + protected function casts(): array { - use HasFactory, Notifiable, Userstamps, HasRoles, softDeletes; - - protected $guard_name = ['web']; - - /** - * The attributes that are mass assignable. - * - * These are the attributes that can be set in bulk during a create or update operation. - * - * @var array - */ - protected $fillable = [ - 'name', - 'email', - 'password', - 'nik', - 'branch_id', - 'profile_photo_path', - 'last_login_at', - 'last_login_ip', - 'sign' + return [ + 'email_verified_at' => 'datetime', + 'password' => 'hashed', + 'id' => 'string', ]; - - /** - * The attributes that should be hidden for serialization. - * - * These are the attributes that will be hidden when the model is converted to an array or JSON. - * - * @var array - */ - protected $hidden = [ - 'password', - 'remember_token', - ]; - - /** - * Get the attributes that should be cast. - * - * This method defines how the attributes should be cast when accessed. - * In this case, 'email_verified_at' is cast to 'datetime', 'password' is cast to 'hashed', and 'id' is cast to 'string'. - * - * @return array - */ - protected function casts() - : array - { - return [ - 'email_verified_at' => 'datetime', - 'password' => 'hashed', - 'id' => 'string', - ]; - } - - public function branch(){ - return $this->belongsTo(Branch::class); - } - - /** - * Create a new factory instance for the model. - * - * @return \Illuminate\Database\Eloquent\Factories\Factory - */ - protected static function newFactory() - { - return \Modules\Usermanagement\Database\Factories\UserFactory::new(); - } } + + public function branch() + { + return $this->belongsTo(Branch::class); + } + + /** + * Create a new factory instance for the model. + * + * @return \Illuminate\Database\Eloquent\Factories\Factory + */ + protected static function newFactory() + { + return \Modules\Usermanagement\Database\Factories\UserFactory::new(); + } + + /** + * Get all of the appointments for the User + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function appointments() + { + return $this->hasMany(Appointment::class, 'admin_id'); + } +}