validate([ 'name' => 'required|string|max:255', 'address' => 'required|string|max:255', 'postcode' => 'required', 'city' => 'required|string|max:255', ]); $shop = Magasin::create($request->except('_token')); return redirect()->route('bo.magasins.edit', $shop->id)->with('success', 'Nouveau Magasin enregistré !'); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $magasin = Magasin::findOrFail($id); $exceptionsCollecte = $magasin->collecteExceptions()->orderBy('date')->get(); return view('bo.magasins.edit', compact('magasin', 'exceptionsCollecte')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $request->validate([ 'name' => 'required|string|max:255', 'address' => 'required|string|max:255', 'postcode' => 'required', 'city' => 'required|string|max:255', ]); $shop = Magasin::findOrFail($id); $shop->fill($request->except('_token'))->save(); return redirect()->route('bo.magasins.edit', $shop->id)->with('success', 'Modifications enregistrés !'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $shop = Magasin::findOrFail($id); $count = Magasin::count(); if ($count === 1) { return redirect()->back()->withErrors("Suppression impossible : Vous devez conserver au moins 1 point de retrait."); } // @todo : controller les crenaux settings liés $shop->delete(); return redirect()->route('bo.magasins.index'); } public function saveExceptions(Request $request) { $collecteException = $request->collecte_exception; for ($i = 0; $i < count($collecteException); $i++) { $collecteExceptionToInsert = !empty($collecteException[$i]["date"]); if (isset($collecteException[$i]["id"]) && $collecteException[$i]["id"] !== null && $collecteExceptionToInsert) { $ce = CollecteException::findOrFail($collecteException[$i]["id"]); $ce->date = $collecteException[$i]["date"]; $ce->comment = $collecteException[$i]["comment"]; $ce->magasin_id = $collecteException[$i]["magasin_id"]; $ce->save(); } else { if ($collecteExceptionToInsert) { $ce = CollecteException::updateOrCreate([ 'date' => $collecteException[$i]["date"], 'comment' => $collecteException[$i]["comment"], 'magasin_id' => $collecteException[$i]["magasin_id"] ]); $collecteException[$i]["id"] = $ce->id; } else { unset($collecteException[$i]); $i--; } } } //remet dans l'ordre avant de renvoyer //$collecteException = CollecteException::where('magasin_id',)->orderBy('date')->get(); $this->arraySortByColumn($collecteException, "date"); return $collecteException; } public function arraySortByColumn(&$array, $column, $direction = SORT_ASC) { $reference_array = array(); foreach($array as $key => $row) { $reference_array[$key] = $row[$column]; } array_multisort($reference_array, $direction, $array); } public function destroyExceptions(Request $request) { $collecteException = CollecteException::findOrFail($request->id); $collecteException->delete(); return "Suppression terminée !"; } }