runDraw($auditions); return; } elseif ($auditions instanceof Collection) { $this->runDrawMultiple($auditions); return; } } public function runDraw(Audition $audition): void { // start off by clearing any existing draw numbers in the audition DB::table('entries')->where('audition_id', $audition->id)->update(['draw_number' => null]); $randomizedEntries = $audition->entries->shuffle(); // Move entries flagged as no show to the end [$noShowEntries, $otherEntries] = $randomizedEntries->partition(function ($entry) { return $entry->hasFlag('no_show'); }); $randomizedEntries = $otherEntries->merge($noShowEntries); // Save draw numbers back to the entries\ $nextNumber = 1; foreach ($randomizedEntries as $index => $entry) { $entry->update(['draw_number' => $nextNumber]); $nextNumber++; } $audition->addFlag('drawn'); } public function runDrawMultiple(Collection $auditions): void { // Eager load the 'entries' relationship on all auditions if not already loaded $auditions->loadMissing('entries'); $auditions->each(fn ($audition) => $this->runDraw($audition)); } }