/** * Crée les entrées de la table Participants correspondant aux équipes */ public function run() { //TODO: à refaire car ca plante si on seed 2 fois car les id de région et de sports sont hardcodés $entrees = [["Arthur", "Archambault", 1, 1, 1, 0], ["Beowulf", "Beaulieu", 2, 1, 2, 0], ["Circé", "Charron", 3, 1, 3, 1], ["Donatello", "DeGrandPré", 4, 2, 1, 0], ["Elsa", "Eiffel", 5, 2, 2, 1], ["Francesco", "Funiculaire", 6, 2, 3, 0], ["Ginette", "Gargantua", 7, 3, 1, 1], ["Henri", "Hippocampe", 8, 3, 2, 0], ["Ivan", "Impitoyable", 9, 3, 3, 0], ["Josephte", "Jamboni", 10, 4, 1, 1], ["Kitty", "KitKat", 11, 4, 2, 1], ["Lola", "Lilalou", 12, 4, 3, 1], ["Manon", "Moriarty", 13, 5, 1, 1], ["Norbert", "Nucléaire", 14, 5, 2, 0], ["Osiris", "Orangeraie", 15, 5, 3, 0], ["Patricia", "Pédoncule", 16, 6, 1, 1], ["Quetzal", "Quelconque", 17, 6, 2, 0], ["Rosa", "Rubéole", 18, 6, 3, 1], ["Stephen", "Satan", 19, 1, 1, 0], ["Tarantula", "Tantrique", 20, 2, 2, 1], ["Ursulin", "Ultime Ninja", 21, 3, 3, 0], ["Vanessa", "Velociraptor", 22, 4, 1, 1], ["Waldorf", "Wolfenstein", 23, 5, 2, 0], ["Xanetia", "Xylophage", 24, 6, 3, 1], ["Yannick", "Ytterbium", 25, 1, 4, 0], ["Zaza", "Zébulon", 26, 2, 4, 1]]; $sports = Sport::all(); $regions = Region::all(); Participant::where('equipe', '=', 0)->delete(); foreach ($entrees as $entree) { $participant = new Participant(); $participant->prenom = $entree[0]; $participant->nom = $entree[1]; $participant->numero = $entree[2]; $participant->region_id = $regions[$entree[3]]->id; $participant->sexe = $entree[5]; $participant->naissance = new DateTime(); $participant->equipe = false; $participant->save(); $participant->sports()->attach([$sports[$entree[4]]->id]); } }
/** * Enregistre dans la bd le participant qui vient d'être créé. * * @return Response */ public function store() { try { $input = Input::all(); $participant = new Participant(); $participant->equipe = false; $participant->nom = $input['nom']; $participant->prenom = $input['prenom']; $participant->telephone = $input['telephone']; $participant->nom_parent = $input['nom_parent']; $participant->numero = $input['numero']; $participant->sexe = $input['sexe']; $participant->adresse = $input['adresse']; $participant->region_id = $input['region_id']; // Création de la date de naissance à partir des valeurs des trois comboboxes $anneeNaissance = $input['annee_naissance'] - 1; $moisNaissance = $input['mois_naissance'] - 1; $jourNaissance = $input['jour_naissance'] - 1; if (checkdate($moisNaissance, $jourNaissance, $anneeNaissance)) { $dateTest = new DateTime(); $dateTest->setDate($anneeNaissance, $moisNaissance, $jourNaissance); $participant->naissance = $dateTest; } else { $participant->naissance = "invalide"; } if ($participant->save()) { if (is_array(Input::get('sport'))) { //FIXME: si le get plante, le save est déjà fait. $participant->sports()->sync(array_keys(Input::get('sport'))); } else { $participant->sports()->detach(); } // Message de confirmation si la sauvegarde a réussi return Redirect::action('ParticipantsController@create')->with('status', 'Le partipant a été créé!'); } else { return Redirect::back()->withInput()->withErrors($participant->validationMessages()); } } catch (Exception $e) { App: abort(404); } }