Ejemplo n.º 1
0
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store(Request $request)
 {
     $nbRes = Reservation::where('user_id', '=', Auth::user()->id)->where('valide', '<=', 1)->count();
     if ($nbRes < 3) {
         $velo = Velo::findOrFail($request->input('idVelo'));
         // Création d'une nouvelle reservation
         $reservation = new Reservation(['valide' => 0]);
         $reservation->user()->associate(Auth::user());
         $reservation->velo()->associate($velo);
         $reservation->save();
         // Création des demi journéées choisies
         $values = $request->all();
         $keys = array_keys($values);
         $newDJ = array();
         unset($keys[0]);
         //on retire le token
         for ($i = 1; $i < sizeof($values) - 2; $i += 2) {
             // -2 pour retirer le veloId
             //control des champs
             $regles = array($keys[$i] => 'required', $keys[$i + 1] => 'required');
             $validation = Validator::make($values, $regles);
             if ($validation->fails()) {
                 Session::flash('info', 'Le formulaire envoyé comportait des champs vides !');
                 $reservation->delete();
                 foreach ($newDJ as $dj) {
                     $dj->delete();
                 }
                 return redirect()->back();
             } else {
                 //formattage de la date
                 $date = explode('-', $values[$keys[$i]]);
                 $date = $date[2] . '-' . $date[1] . '-' . $date[0];
                 $demijournee = new Demijournee(['date' => $date, 'periode' => $values[$keys[$i + 1]]]);
                 $demijournee->reservation()->associate($reservation);
                 $demijournee->save();
                 $newDJ[] = $demijournee;
             }
         }
         return redirect()->route('location.show', ['location' => $reservation->id]);
     } else {
         Session::flash('info', 'Vous ne pouvez pas avoir plus de trois réservations en attente ! Merci de votre compréhension.');
         return redirect()->back();
     }
 }