'datetime', 'used_at' => 'datetime', ]; /** * Relationship: User */ public function user(): BelongsTo { return $this->belongsTo(User::class); } /** * Create a new verification token for user */ public static function createForUser(User $user, int $expiresInHours = 24): self { // Invalidate existing tokens self::where('user_id', $user->id)->whereNull('used_at')->delete(); return self::create([ 'user_id' => $user->id, 'token' => Str::random(64), 'expires_at' => now()->addHours($expiresInHours), ]); } /** * Find valid token */ public static function findValid(string $token): ?self { return self::where('token', $token) ->where('expires_at', '>', now()) ->whereNull('used_at') ->first(); } /** * Check if token is valid */ public function isValid(): bool { return $this->expires_at > now() && $this->used_at === null; } /** * Mark token as used */ public function markAsUsed(): void { $this->update(['used_at' => now()]); } }