/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $data = $request->only('name', 'price'); try { $option = Option::find($id); $option->update($data); return $option; } catch (Exception $e) { return $e; } }
public function edit(Request $request, $id) { $product = Product::find($id); /** * Validate the submitted Data */ $this->validate($request, ['name' => 'required', 'manufacturer' => 'required', 'price' => 'required', 'details' => 'required', 'quantity' => 'required', 'categories' => 'required', 'thumbnail' => 'image']); if ($request->hasFile('album')) { foreach ($request->album as $photo) { if ($photo && strpos($photo->getMimeType(), 'image') === false) { return \Redirect()->back(); } } } /** * Remove the old categories from the pivot table and maintain the reused ones */ $added_categories = []; foreach ($product->categories as $category) { if (!in_array($category->id, $request->categories)) { CategoryProduct::whereProduct_id($product->id)->whereCategory_id($category->id)->delete(); } else { $added_categories[] = $category->id; } } /** * Link the new categories to the pivot table */ foreach ($request->categories as $category_id) { if (!in_array($category_id, $added_categories)) { CategoryProduct::create(['category_id' => $category_id, 'product_id' => $product->id]); } } $info = $request->all(); /** * Upload a new thumbnail and delete the old one */ $dest = "content/images/"; if ($request->file('thumbnail')) { File::delete(public_path() . $product->thumbnail); $name = str_random(11) . "_" . $request->file('thumbnail')->getClientOriginalName(); $request->file('thumbnail')->move($dest, $name); $info['thumbnail'] = "/" . $dest . $name; } /** * Upload Album Photos */ if ($request->hasFile('album')) { foreach ($request->album as $photo) { if ($photo) { $name = str_random(11) . "_" . $photo->getClientOriginalName(); $photo->move($dest, $name); AlbumPhoto::create(['product_id' => $product->id, 'photo_src' => "/" . $dest . $name]); } } } $product->update($info); /** * Linking the options to the product */ if ($request->has('options')) { foreach ($request->options as $option_details) { if (!empty($option_details['name']) && !empty($option_details['values']['name'][0])) { if (isset($option_details['id'])) { $size = count($option_details['values']['id']); Option::find($option_details['id'])->update(['name' => $option_details['name']]); foreach ($option_details['values']['name'] as $key => $value) { if ($key < $size) { OptionValue::find($option_details['values']['id'][$key])->update(['value' => $value]); } else { OptionValue::create(['value' => $value, 'option_id' => $option_details['id']]); } } } else { $option = Option::create(['name' => $option_details['name'], 'product_id' => $product->id]); foreach ($option_details['values']['name'] as $value) { if (!empty($value)) { OptionValue::create(['value' => $value, 'option_id' => $option->id]); } } } } } } return \Redirect()->back()->with(['flash_message' => 'Product Successfully Modified']); }
public function destroyOpt($id) { $option = Option::find($id); $option->delete(); return redirect(route('categoryIndex')); }