-
app/Models/Recipe.php
Open in GitHubuse Illuminate\Database\Eloquent\Model; class Recipe extends Model { // public function ingredients() { return $this->belongsToMany(Ingredient::class)->withPivot('quantity'); } // }
-
app/Http/Controllers/RecipeController.php
Open in GitHubuse App\Http\Requests\StoreRecipeRequest; class RecipeController extends Controller { // public function store(StoreRecipeRequest $request): RedirectResponse { $recipe = Auth::User()->Recipes()->create([ 'name' => $request['name'], 'servings' => $request['servings'], 'adults' => $request->has('adults'), 'kids' => $request->has('kids'), 'timing' => $request['timing'], 'category_id' => $request['category_id'], 'instruction' => $request['instruction'] ]); $file = TempFile::where('folder', $request->image)->first(); if ($file) { $recipe->addMedia(Storage::path($request->image . '/' . $file->filename))->toMediaCollection(); $file->delete(); } for($i = 0; $i < count($request['ingredients']); $i++) { $recipe->ingredients()->attach($request['ingredients'][$i], ['quantity' => $request['quantities'][$i]]); } foreach($request['allergens'] as $id => $level) { if($level != 'no') { $recipe->allergens()->attach($id, ['level' => $level]); } } return redirect($recipe->path()); } // }