'boolean', 'expires_at' => 'datetime', 'used_at' => 'datetime', ]; public function user(): BelongsTo { return $this->belongsTo(User::class); } public function isAnonymous(): bool { return is_null($this->user_id); } public function isExpired(): bool { return $this->expires_at->isPast(); } public function isUsed(): bool { return ! is_null($this->used_at); } public function isValid(): bool { return ! $this->isExpired() && ! $this->isUsed(); } public function markAsUsed(): void { $this->update(['used_at' => now()]); } public function scopeExpiredAndUnused($query) { return $query->where('expires_at', '<', now()) ->whereNull('used_at'); } public function scopeForUser($query, $userId) { return $query->where('user_id', $userId); } }