UpdateStudentStatus.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Student;
  4. use Illuminate\Console\Command;
  5. class UpdateStudentStatus extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'students:update-status';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Updates students status based on their contracts validity';
  19. /**
  20. * Execute the console command.
  21. */
  22. public function handle(): void
  23. {
  24. $this->info('Starting student status update...');
  25. // Using chunk to avoid memory exhaustion on large databases
  26. $updatedCount = 0;
  27. Student::with('contracts')->chunkById(200, function ($students) use (&$updatedCount) {
  28. foreach ($students as $student) {
  29. $oldStatus = $student->status;
  30. $student->updateStatus();
  31. if ($oldStatus !== $student->status) {
  32. $updatedCount++;
  33. }
  34. }
  35. });
  36. $this->info("Student status update finished. {$updatedCount} students updated.");
  37. }
  38. }