/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $recipeIngredient = RecipeIngredient::find($id); $code = 'NOT_OK'; if (!empty($recipeIngredient) && RecipeIngredient::destroy($id)) { $code = 'OK'; } return response()->json(['code' => $code]); }
/** * Creates all recipe ingredients * * @param Request $request * @param int|string $recipeId */ private function saveIngredients(Request $request, $recipeId) { foreach ($request->get('ingredient') as $ingredient) { if (!$request->exists('amount-' . $ingredient)) { continue; } // creates new recipe ingredients if (is_numeric($ingredient)) { if (Ingredient::exists($ingredient) === false) { continue; } if ($request->exists('updateIngredient')) { DB::enableQueryLog(); if (in_array($ingredient, $request->get('updateIngredient'))) { $recipeIngredient = RecipeIngredient::where(function ($query) use($recipeId, $ingredient) { $query->where('recipe_id', '=', $recipeId)->where('ingredient_id', '=', $ingredient); })->first(); $recipeIngredient->amount = $request->get('amount-' . $ingredient); $recipeIngredient->save(); continue; } } $recipeIngredient = new RecipeIngredient(); $recipeIngredient->recipe_id = $recipeId; $recipeIngredient->ingredient_id = $ingredient; $recipeIngredient->amount = $request->get('amount-' . $ingredient); $recipeIngredient->save(); } else { $newIngredient = new Ingredient(); $newIngredient->name = ucfirst(str_replace('-', ' ', $ingredient)); if ($newIngredient->save()) { $recipeIngredient = new RecipeIngredient(); $recipeIngredient->recipe_id = $recipeId; $recipeIngredient->ingredient_id = $newIngredient->id; $recipeIngredient->amount = $request->get('amount-' . $ingredient); $recipeIngredient->save(); } } } }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { Recipe::destroy($id); RecipeIngredient::where('recipe_id', $id)->delete(); RecipeStep::where('recipe_id', $id)->delete(); return redirect()->route('recipe.index'); }
public function update(Request $request) { $recipe = Recipe::find($request->id); $recipe->name = $request->name; $recipe->prep_time = $request->prep_time; $recipe->cook_time = $request->cook_time; $recipe->serving_size = $request->serving_size; $recipe->instructions = $request->instructions; $recipe->save(); foreach ($request->ingredients as $item) { if (isset($item['ri_id'])) { $recipeIngrediet = RecipeIngredient::find($item['ri_id']); $recipeIngrediet->unit = $item['unit']; $recipeIngrediet->ingredient_id = $item['id']; $recipeIngrediet->save(); } else { $recipeIngrediet = new RecipeIngredient(); $recipeIngrediet->unit = $item['unit']; $recipeIngrediet->recipe_id = $request->id; $recipeIngrediet->ingredient_id = $item['id']; $recipeIngrediet->save(); } } echo json_encode(true); exit; }