/**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     $ingredients = Ingredient::get();
     return view('manager.list-ingredients', compact('ingredients'));
 }
 public function update(Request $request, $id)
 {
     $product = Product::findOrFail($id);
     $allIngredients = Ingredient::get()->toArray();
     $ingredientsInProduct = $product->ingredients->toArray();
     $product->update($request->all());
     foreach ($ingredientsInProduct as $ingredient) {
         if (array_search($ingredient['name'], $request->input('ingredient-list')) === false) {
             IngredientsInProduct::where('product_id', $id)->where('ingredient_id', $ingredient['id'])->delete();
         }
     }
     foreach ($request->input('ingredient-list') as $inputIngredient) {
         if (is_int($this->ingredientSearch($inputIngredient, $ingredientsInProduct, 'name'))) {
         } elseif (is_int($this->ingredientSearch($inputIngredient, $allIngredients, 'name'))) {
             $attachIngredientToProduct = new IngredientsInProduct();
             $attachIngredientToProduct->product_id = $id;
             $attachIngredientToProduct->ingredient_id = $allIngredients[array_search($inputIngredient, array_column($allIngredients, 'name'))]['id'];
             $attachIngredientToProduct->save();
         } else {
             $newIngredient = new Ingredient();
             $newIngredient->name = $inputIngredient;
             $newIngredient->unit = 'stuks';
             $newIngredient->type = 'groente';
             $newIngredient->min_amount = 1;
             $newIngredient->save();
             $attachIngredientToProduct = new IngredientsInProduct();
             $attachIngredientToProduct->product_id = $id;
             $attachIngredientToProduct->ingredient_id = $newIngredient->id;
             $attachIngredientToProduct->save();
         }
     }
     return redirect()->action('ProductsController@edit', [$id])->with('status', 'Ingredient update is geslaagd!');
 }
예제 #3
0
 public function getEdit($id = null)
 {
     $beer = \App\Beer::find($id);
     $recipe = \App\Recipe::where('beer_id', $id)->get();
     $ingredients = \App\Ingredient::get();
     if (is_null($beer)) {
         \Session::flash('flash_message', 'Beer not found.');
         return redirect('\\beer');
     }
     # array to populate a drop down on edit blade
     $measures = ['oz', 'lb', 'tsp', 'tbsp', 'package'];
     return view('beer.edit')->with('beer', $beer)->with('recipe', $recipe)->with('ingredients', $ingredients)->with('measures', $measures);
 }