where('status', Visit::STATUS_CHECKED_IN) ->whereNotNull('badge_expires_at') ->where('badge_expires_at', '<', now()) ->with(['visitor', 'organization']) ->get(); $count = 0; foreach ($visits as $visit) { $alreadyLogged = AuditLog::query() ->where('subject_type', Visit::class) ->where('subject_id', $visit->id) ->where('action', 'badge.expired') ->where('created_at', '>=', now()->subDay()) ->exists(); if ($alreadyLogged) { continue; } AuditLog::record( $visit->owner_ref, 'badge.expired', $visit->organization_id, null, Visit::class, $visit->id, [ 'visitor' => $visit->visitor->full_name, 'badge_code' => $visit->badge_code, 'expired_at' => $visit->badge_expires_at?->toIso8601String(), ], ); $notifications->badgeExpired($visit); $count++; } $this->info("Recorded {$count} expired badge alert(s)."); return self::SUCCESS; } }