|
|
@@ -0,0 +1,47 @@
|
|
|
+<?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.");
|
|
|
+ }
|
|
|
+}
|