/** * API for adding materials being called from js */ public function addMaterial(Request $request) { $prodId = $request->input('sub_product_id'); $matId = $request->input('material_id'); $matPrice = $request->input('price'); //add material product $prodMat = new SubProductMaterial(); $prodMat->sub_product_id = $prodId; $prodMat->material_id = $matId; $prodMat->mat_sub_price = $matPrice; $prodMat->is_active = true; $prodMat->save(); //get the product-materials $curMaterials = array(); $curMatIds = array(); $prodMaterials = SubProduct::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)); }