where('type', UserType::Client) ->where('active', true) ->whereNotNull('expires_at') ->where('expires_at', '<=', $now) ->get(['id', 'name', 'expires_at']); foreach ($due as $client) { // Conditional on the row still being due, not a plain save(): // an administrator who moved the date forward between the read // above and this write has just decided the account should keep // working, and must not be overruled by a list that is a few // milliseconds old. $switchedOff = User::query() ->whereKey($client->id) ->where('active', true) ->where('expires_at', '<=', $now) ->update(['active' => false]); if ($switchedOff === 1) { $activity->logSystem(Action::ClientExpired, ['name' => $client->name, 'id' => $client->id]); $expired++; } } $this->info("Deactivated {$expired} expired client account(s)."); return self::SUCCESS; } }