public function create() { $data = ['name' => Input::get('name'), 'title' => Input::get('title'), 'description' => Input::get('description'), 'category_id' => Input::get('category_id'), 'price' => Input::get('price'), 'VAT' => Input::get('VAT'), 'size' => Input::get('size'), 'color' => Input::get('color'), 'material' => Input::get('material'), 'details' => Input::get('details'), 'weight' => Input::get('weight'), 'stock' => Input::get('stock')]; if (isset($data) && is_array($data)) { $product = new Product(); if ($product->validate($data)) { $product->name = $data['name']; $product->title = $data['title']; $product->description = $data['description']; $product->category_id = $data['category_id']; $product->price = $data['price']; $product->VAT = $data['VAT']; $product->size = $data['size']; $product->color = $data['color']; $product->material = $data['material']; $product->details = $data['details']; $product->weight = $data['weight']; $product->stock = $data['stock']; if (Input::file('image')->isValid()) { $fileName = rand(11111, 99999) . '.' . Input::file('image')->getClientOriginalExtension(); Input::file('image')->move('images/products/' . $product->id . '/', $fileName); $product->image = $product->id . '/' . $fileName; } else { return Redirect::route('products')->with(['msg' => 'Failed to create!']); } $create = $product->save(); if (!$create) { return Redirect::route('products')->with(['msg' => 'Failed to create!']); } return Redirect::route('products')->with(['msg' => 'Successfully created!']); } return Redirect::route('products')->with(['msg' => 'Failed to create!']); } return Redirect::route('products')->with(['msg' => 'Failed to create!']); }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $a = new Product(); if (!$a->validate(Input::all())) { return redirect('product/create')->withErrors($a->errors())->withInput(); } $a->fill(Input::all()); $a->save(); Flash::success('New product is created'); return Redirect::to('product'); }
private function saveProduct(Request $request, Product $product) { $validator = Product::validate($request->all()); if ($validator->fails()) { return redirect(route(Route::currentRouteName()))->withErrors($validator->errors()); } else { if (!is_null($product->id)) { $product->update($request->all()); Session::flash('success', "Le produit a bien été mis à jour."); } else { $product->fill($request->all()); $product->save(); Session::flash('success', "Le produit a bien été crée."); } } if ($param = Route::current()->getParameter('id')) { return redirect()->route(Route::currentRouteName(), $param); } return redirect()->route(Route::currentRouteName()); }