/** * API or removing materials being called from js */ public function removeMaterial(Request $request) { $prodId = $request->input('product_id'); $matId = $request->input('material_id'); //delete material product ProductMaterial::where('product_id', $prodId)->where('material_id', $matId)->delete(); //get the product-materials $curMaterials = array(); $curMatIds = array(); $prodMaterials = Product::find($prodId)->materials()->get(); foreach ($prodMaterials as $pm) { //gets the current set materials $matExtObject = Material::where('is_active', true)->where('id', $pm->material_id)->first(); array_push($curMaterials, $matExtObject); array_push($curMatIds, $pm->material_id); } //get the all current materials $allMaterials = Material::whereNotIn('id', $curMatIds)->where('is_active', True)->orderBy('material_name', 'asc')->get(); //return current and all materials return json_encode(array('curMaterials' => $curMaterials, 'allMaterials' => $allMaterials)); }
/** * 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, ['material_name' => 'required|max:255|min:3', 'material_desc' => 'required|min:10', 'material_code' => 'required|min:3']); $is_active = $request->input('is_active') ? true : false; $matObj = new Material(); $matObj->where('id', $id)->update(['material_name' => $request->input('material_name'), 'material_desc' => $request->input('material_desc'), 'material_categ_id' => $request->input('material_category'), 'material_code' => $request->input('material_code'), '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/material/edit/{$id}")->with('message', $request->input('material_name') . ' was successfully updated'); } catch (Exception $e) { return Redirect::to("/back/material/edit/{$id}")->with('message', 'Oops! Something went wrong. Please try again later'); } }