*/ protected $hidden = [ 'legacy_user_id', 'legacy_institution_id', ]; /** * @return array */ protected function casts(): array { return [ 'institution_id' => 'integer', 'user_id' => 'integer', 'user_position_id' => 'integer', 'start_year' => 'integer', 'end_year' => 'integer', 'is_open' => 'boolean', 'legacy_user_id' => 'integer', 'legacy_institution_id' => 'integer', ]; } /** * @return BelongsTo */ public function institution(): BelongsTo { return $this->belongsTo(Institution::class); } /** * @return BelongsTo */ public function user(): BelongsTo { return $this->belongsTo(User::class); } /** * @return BelongsTo */ public function userPosition(): BelongsTo { return $this->belongsTo(UserPosition::class); } /** * Esiste giĆ  un'altra affiliazione aperta per lo stesso ente+utente: un * restore qui urterebbe l'indice unique su (institution_id, user_id, is_open). */ public function hasConflictingOpenAffiliation(): bool { if (filled($this->end_year)) { return false; } return self::query() ->where('institution_id', $this->institution_id) ->where('user_id', $this->user_id) ->where('id', '!=', $this->id) ->whereNotNull('is_open') ->exists(); } }