option('dry-run'); $query = User::query() ->whereNotNull('birthday') ->where('birthday', 'like', '____-__-01'); $total = (clone $query)->count(); if ($total === 0) { $this->info('未找到需要修复的生日数据'); return self::SUCCESS; } $this->info(($dryRun ? '预览' : '开始修复') . " {$total} 条生日数据"); $updated = 0; $previewed = 0; $query->orderBy('id')->chunkById(200, function ($users) use ($dryRun, &$updated, &$previewed) { foreach ($users as $user) { $normalizedBirthday = substr($user->birthday, 0, 7); if ($dryRun) { if ($previewed < 20) { $this->line("user_id={$user->id} {$user->birthday} => {$normalizedBirthday}"); } $previewed++; continue; } $user->birthday = $normalizedBirthday; $user->save(); $updated++; } }); if ($dryRun) { if ($previewed > 20) { $this->line('仅展示前 20 条预览结果'); } $this->info("预览完成,共 {$total} 条待修复"); return self::SUCCESS; } $this->info("修复完成,共更新 {$updated} 条生日数据"); return self::SUCCESS; } }