/** * API or removing materials being called from js */ public function removeSubProduct(Request $request) { $prodId = $request->input('product_id'); $subProdId = $request->input('sub_product_id'); //delete sub product MainSubProduct::where('product_id', $prodId)->where('sub_product_id', $subProdId)->delete(); //get all subproducts associated $curSubProducts = array(); $curSubProductIds = array(); $subProducts = Product::find($prodId)->sub_products()->get(); foreach ($subProducts as $pm) { //gets the current set materials $subprodExtObject = SubProduct::where('is_active', true)->where('id', $pm->sub_product_id)->first(); array_push($curSubProducts, $subprodExtObject); array_push($curSubProductIds, $pm->sub_product_id); $subprodExtObject = null; } //get all the subproducts not in current main product $allSubProducts = SubProduct::whereNotIn('id', $curSubProductIds)->orderBy('sub_product_name', 'asc')->get(); //return current and all materials return json_encode(array('curSubProducts' => $curSubProducts, 'allSubProducts' => $allSubProducts)); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { try { $this->validate($request, ['sub_product_name' => 'required|max:255|min:3', 'sub_product_desc' => 'required|min:1|max:500', 'price' => 'required|numeric', 'size' => 'required|numeric']); $is_active = $request->input('is_active') ? true : false; $productobj = new SubProduct(); $productobj->where('id', $request->input('id'))->update(['sub_product_name' => $request->input('sub_product_name'), 'sub_product_desc' => $request->input('sub_product_desc'), 'size' => $request->input('size'), 'price' => $request->input('price'), 'is_active' => $is_active]); //update attachment images if ($request->has('img')) { foreach ($request->input('img') as $img) { //unserialize image value $imgVal = unserialize($img); if ($imgVal[1]) { $fileObj = new Files(); $fileObj->where('id', $imgVal[0])->update(['attachment_id' => $id]); } else { $fileObj = new Files(); $fileObj->where('id', $imgVal[0])->update(['is_active' => false]); } } } return Redirect::to("/back/subproduct/edit/{$id}")->with('message', $request->input('sub_product_name') . ' was successfully updated'); } catch (Exception $e) { return Redirect::to("/back/subproduct/edit/{$id}")->with('message', 'Oops! Something went wrong. Please try again later'); } }