*/ use HasFactory, Notifiable; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', 'role' => Role::class, 'is_active' => 'boolean', 'last_login_at' => 'datetime', ]; } /** @return HasOne */ public function superuser(): HasOne { return $this->hasOne(Superuser::class); } /** @return HasOne */ public function admin(): HasOne { return $this->hasOne(Admin::class); } /** @return HasOne */ public function operator(): HasOne { return $this->hasOne(Operator::class); } /** @return HasOne */ public function dosen(): HasOne { return $this->hasOne(Dosen::class); } /** @return HasOne */ public function mahasiswa(): HasOne { return $this->hasOne(Mahasiswa::class); } /** * Profil sesuai role akun ini (relasi dimuat bila belum, aman untuk strict mode). * * @return Attribute */ protected function profile(): Attribute { return Attribute::get(function (): ?Model { $relation = $this->role->profileRelation(); return $this->loadMissing($relation)->getRelation($relation); })->withoutObjectCaching(); } public function hasRole(Role|string ...$roles): bool { foreach ($roles as $role) { $role = $role instanceof Role ? $role : Role::tryFrom($role); if ($role !== null && $this->role === $role) { return true; } } return false; } /** Dua huruf inisial dari nama, mengabaikan gelar yang berakhiran titik (Dr., Ir.) dan gelar belakang. */ public function initials(): string { $name = Str::before($this->name, ','); $words = collect(preg_split('/\s+/', trim($name), -1, PREG_SPLIT_NO_EMPTY)); $given = $words->reject(fn (string $word): bool => str_ends_with($word, '.')); if ($given->isEmpty()) { $given = $words; } return Str::upper($given->take(2)->map(fn (string $word): string => Str::substr($word, 0, 1))->implode('')); } }