public function store(Request $request) { $ingredient = Ingredient::where('name', $request->get('name'))->first(); if (!$ingredient) { $ingredient = new Ingredient(); $ingredient->fill($request->all()); $ingredient->save(); } $ingredient->recipes()->attach($request->get('recipe_id'), ['quantity' => $request->get('quantity')]); return $ingredient; }