public function postUpdate($tournament) { if ($tournament->user != Auth::user()->id) { return Redirect::to('tournaments'); } $tournament->name = Input::get('name'); $maps = array_keys(Input::get('maps', [])); $players = array_keys(Input::get('players.ids', [])); $names = array_keys(Input::get('players.names', [])); if (is_null($tournament->name)) { return Redirect::back(); } $total = count($names) + count($players); if (ceil($total / 2) != count($maps)) { return Redirect::back(); } $tournament->save(); $tournament->maps()->detach(); foreach ($maps as $map) { $tournament->maps()->attach($map); } $tournament->players()->detach(); foreach ($players as $player) { $tournament->players()->attach($player); } $scenarii = new Collection(); $tournament->maps->each(function ($map) use(&$scenarii, $tournament) { if ($scenarii->count() < 2) { $scenarii = Scenario::where('season', Carbon::now()->year)->get()->shuffle(); } $map->scenarii($tournament)->attach($scenarii->pop()->id, ['tournament' => $tournament->id]); $map->scenarii($tournament)->attach($scenarii->pop()->id, ['tournament' => $tournament->id]); }); foreach ($names as $name) { $player = new Player(); $player->name = $name; $player->user = Auth::user()->id; $player->save(); $tournament->players()->attach($player->id); } if ($total % 2 != 0) { $tournament->players()->attach(User::fantom()->id); } return Redirect::to('tournaments/' . $tournament->id); }