/**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $recipe = Recipe::find($id);
     $recipe->title = $request->input('title');
     $recipe->description = $request->input('description');
     $recipe->servings = $request->input('servings');
     $recipe->prep_time = $request->input('prep_time');
     $recipe->cook_time = $request->input('cook_time');
     $recipe->save();
     // Get the recipe's current ingredients.
     $recipeIngredients = RecipeIngredient::where('recipe_id', $id)->get();
     $currentIngredients = [];
     foreach ($recipeIngredients as $ingredient) {
         $currentIngredients[] = $ingredient->ingredient;
     }
     $newIngredients = $request->input('ingredient');
     $totalIngredients = count($newIngredients);
     $amounts = $request->input('amount');
     $units = $request->input('unit');
     // Loop through the amount of total ingredients, including any new.
     for ($i = 0; $i < $totalIngredients; $i++) {
         $recipeIngredient = RecipeIngredient::where('recipe_id', $id)->where('ingredient', $currentIngredients[$i])->first();
         // Create a new ingredient if a new one has been added.
         if (is_null($recipeIngredient)) {
             $recipeIngredient = new RecipeIngredient();
             $recipeIngredient->recipe_id = $id;
         }
         // Set/update ingredient values.
         $recipeIngredient->ingredient = $newIngredients[$i];
         $recipeIngredient->amount = $amounts[$i];
         $recipeIngredient->unit = $units[$i];
         $recipeIngredient->save();
     }
     $order = 1;
     foreach ($request->input('step') as $step) {
         $recipeStep = RecipeStep::where(['recipe_id' => $id, 'order' => $order])->orderBy('order', 'asc')->first();
         if (is_null($recipeStep)) {
             $recipeStep = new RecipeStep();
             $recipeStep->recipe_id = $id;
         }
         $recipeStep->order = $order;
         $recipeStep->content = $step;
         $recipeStep->save();
         $order++;
     }
     return redirect()->route('recipe.index');
 }
Exemple #2
0
 /**
  * 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();
             }
         }
     }
 }
 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;
 }