/** * Ajout à la base de donnée d'une nouvelle commande et redirige vers * l'index une fois l'ajout fait. * * @return \Illuminate\Http\Response */ public function store() { try { $input = Input::all(); if (Input::has('maxLineCount')) { $lineCount = $input['maxLineCount']; } else { $lineCount = 0; } $clients = Client::lists('id'); /* Creation de l'objet Commande et assigne les attributs */ $commande = new Commande(); $commande->clients_Id = $clients[$input['clientsId']]; $commande->dateDebut = new DateTime($input['dateDebut']); $commande->dateFin = new DateTime($input['dateFin']); $commande->etat = $input['etat']; $commande->commentaire = $input['commentaire']; } catch (ModelNotFoundException $e) { App::abort(404); } if ($commande->save()) { try { /** * Attache les produits finis à la commande. * Retrouve le "name" de chaque infos de chaque produits finis * et attache le produit à la commande. */ for ($i = 1; $i <= $lineCount; $i++) { $code = $i . "_code"; if (Input::has($code)) { $pointure = $i . "_pointure"; $quantite = $i . "_quantite"; $produitFini = DB::table('ProduitsFinis')->where('code', $input[$code])->first(); $commande->ProduitsFinis()->attach($produitFini->id, ['pointure' => $input[$pointure], 'quantite' => $input[$quantite]]); } } } catch (ModelNotFoundException $e) { App::abort(404); } return Redirect::action('CommandesController@index'); } else { return Redirect::back()->withInput()->withErrors($commande->validationMessages()); } }