/** * Store a newly created resource in storage. * * @return Response */ public function store(ProductRequest $request) { // $thumb = public_path('images/products/thumb'); $full = public_path('images/products/full'); try { $input = $request->except('image'); $input['product_price'] = str_replace('.', '', $input['product_price']); $cat_slug = Category::find($input['id_category'])->slug; $input['slug'] = $cat_slug . '/' . str_slug($input['product_name']); $input['status'] = $request->get('status') == 'on' ? 1 : 0; $attr = array_filter($input['name']); $product = new Product($input); $product->save(); $pro_id = $product->id; if (!empty($attr)) { Attribute::SaveAttribute($pro_id, $input['name'], $input['value']); } if ($request->hasFile('image')) { $images = $request->file('image'); foreach ($images as $image) { $name = str_random(5) . '.' . $image->getClientOriginalExtension(); $img = new Gambar(); $img->img_name = $name; $img->id_product = $pro_id; $img->path_thumb = 'images/products/thumb/' . $name; $img->path_full = 'images/products/full/' . $name; $img->save(); Image::make($image)->save($full . '/' . $name); Image::make($image)->resize('100', '100')->save($thumb . '/' . $name); } } } catch (Exception $exc) { $message = $e->getMessage(); } if (isset($message)) { return redirect()->back()->withInput()->withErrors(['message' => $message]); } return redirect()->route('backend.product.index'); }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { // $gambar = Gambar::find($id); if (Storage::exists('products/thumb/' . $gambar->img_name)) { Storage::delete('products/thumb/' . $gambar->img_name); } if (Storage::exists('products/full/' . $gambar->img_name)) { Storage::delete('products/full/' . $gambar->img_name); } if ($gambar->delete()) { return response()->json(['success' => TRUE]); } }