Role::class, "last_active_at" => "datetime", "employment_form" => EmploymentForm::class, "employment_date" => "date", ]; protected $hidden = [ "remember_token", ]; protected $with = [ "profile", ]; protected $perPage = 50; public function profile(): HasOne { return $this->hasOne(Profile::class); } public function vacationLimits(): HasMany { return $this->hasMany(VacationLimit::class); } public function vacationRequests(): HasMany { return $this->hasMany(VacationRequest::class); } public function createdVacationRequests(): HasMany { return $this->hasMany(VacationRequest::class, "creator_id"); } public function vacations(): HasMany { return $this->hasMany(Vacation::class); } public function keys(): HasMany { return $this->hasMany(Key::class); } public function hasRole(Role $role): bool { return $this->role === $role; } public function hasVacationLimit(YearPeriod $yearPeriod): bool { return $this->vacationLimits() ->whereBelongsTo($yearPeriod) ->whereNotNull("days") ->exists(); } public function scopeSearch(Builder $query, ?string $text): Builder { if ($text === null) { return $query; } return $query ->where("email", "ILIKE", "%{$text}%") ->orWhereRelation( "profile", fn(Builder $query): Builder => $query ->where("first_name", "ILIKE", "%{$text}%") ->orWhere("last_name", "ILIKE", "%{$text}%"), ); } public function scopeOrderByProfileField(Builder $query, string $field): Builder { $profileQuery = Profile::query()->select($field)->whereColumn("users.id", "profiles.user_id"); return $query->orderBy($profileQuery); } public function scopeWithVacationLimitIn(Builder $query, YearPeriod $yearPeriod): Builder { return $query->whereRelation( "vacationlimits", fn(Builder $query): Builder => $query ->whereBelongsTo($yearPeriod) ->whereNotNull("days"), ); } public function routeNotificationForSlack() { return $this->profile->slack_id; } protected static function newFactory(): UserFactory { return UserFactory::new(); } }