/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { //validate this var_dump($request->all()); $input = $request->all(); $params = []; $quantities = $input['quantities'] = explode(',', $input['quantities']); $items = $this->item->getIn('id', explode(',', $input['ids'])); $ctr = 0; $recipe = Recipe::create($request->all()); foreach ($items as $key => $value) { $ing = ['name' => $value['name'] . " for " . $recipe->name, 'recipe_id' => $recipe->id, 'item_id' => $value['id'], 'quantity' => (int) $quantities[$ctr]]; array_push($params, $ing); $ctr++; } $ingredients = Ingredient::insert($params); if ($recipe && $ingredients) { return redirect()->back()->with('flash_message', 'Recipe has been successfully saved.'); } $this->recipe->fdelete($recipe); foreach ($ingredients as $key) { $key->forceDelete(); } return redirect()->back()->withErrors('Could not save recipe'); }