/** * Random draw teams for the event based on the players * * @return redirect **/ public function drawTeams(Event $event, $createMatch = false) { // remove event teams first $event->teams()->delete(); $playersPerTeam = $event->type == 'doubles' ? 2 : 1; $chunkedPlayers = $event->players->shuffle()->chunk($playersPerTeam); $teamNames = []; foreach ($chunkedPlayers as $chunk) { $team = new Team(); do { $name = $team->getRandomTeamName(); } while (in_array($name, $teamNames)); $team->name = $name; $team->save(); $team->players()->saveMany($chunk); $event->teams()->save($team); $teamNames[] = $team->name; } // should this redirect to create a match? if ($createMatch) { return redirect()->route('dashboard.events.addMatch', $event)->with('app-success', 'Teams have been generated randomly.'); } return redirect()->route('dashboard.events.single', $event)->with('app-success', 'Teams have been generated randomly.'); }