| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- namespace App\Console\Commands;
- use App\Models\Student;
- use Illuminate\Console\Command;
- class UpdateStudentStatus extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'students:update-status';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Updates students status based on their contracts validity';
- /**
- * Execute the console command.
- */
- public function handle(): void
- {
- $this->info('Starting student status update...');
- // Using chunk to avoid memory exhaustion on large databases
- $updatedCount = 0;
-
- Student::with('contracts')->chunkById(200, function ($students) use (&$updatedCount) {
- foreach ($students as $student) {
- $oldStatus = $student->status;
- $student->updateStatus();
-
- if ($oldStatus !== $student->status) {
- $updatedCount++;
- }
- }
- });
- $this->info("Student status update finished. {$updatedCount} students updated.");
- }
- }
|