*/ use HasFactory; /** * Bootstrap the model and its traits. */ protected static function boot(): void { parent::boot(); static::creating(function (TeamInvitation $invitation) { if (empty($invitation->code)) { $invitation->code = Str::random(64); } }); } /** * Get the team that the invitation belongs to. * * @return BelongsTo */ public function team(): BelongsTo { return $this->belongsTo(Team::class); } /** * Get the user who sent the invitation. * * @return BelongsTo */ public function inviter(): BelongsTo { return $this->belongsTo(User::class, 'invited_by'); } /** * Determine if the invitation has been accepted. */ public function isAccepted(): bool { return $this->accepted_at !== null; } /** * Determine if the invitation is pending. */ public function isPending(): bool { return $this->accepted_at === null && ! $this->isExpired(); } /** * Determine if the invitation has expired. */ public function isExpired(): bool { return $this->expires_at !== null && $this->expires_at->isPast(); } /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'role' => TeamRole::class, 'expires_at' => 'datetime', 'accepted_at' => 'datetime', ]; } /** * Get the route key for the model. */ public function getRouteKeyName(): string { return 'code'; } }