with('foods', Food::all()->sortBy('name')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Contracts\View\View */ public function create(): View { return view('foods.create') ->with('serving_units', new Collection([ ['value' => 'tsp', 'label' => 'tsp.'], ['value' => 'tbsp', 'label' => 'tbsp.'], ['value' => 'cup', 'label' => 'cup'], ])); } /**newly * Store a newly created resource in storage. */ public function store(Request $request): RedirectResponse { $attributes = $request->validate([ 'name' => 'required|string', 'detail' => 'nullable|string', 'brand' => 'nullable|string', 'serving_size' => 'required|numeric', 'serving_unit' => 'nullable|string', 'serving_weight' => 'required|numeric', 'calories' => 'nullable|numeric', 'fat' => 'nullable|numeric', 'cholesterol' => 'nullable|numeric', 'sodium' => 'nullable|numeric', 'carbohydrates' => 'nullable|numeric', 'protein' => 'nullable|numeric', ]); /** @var \App\Models\Food $food */ $food = tap(new Food(array_filter($attributes)))->save(); return back()->with('message', "Food {$food->name} added!"); } /** * Display the specified resource. * * @param \App\Models\Food $food * @return \Illuminate\Http\Response */ public function show(Food $food) { // } /** * Show the form for editing the specified resource. * * @param \App\Models\Food $food * @return \Illuminate\Http\Response */ public function edit(Food $food) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Models\Food $food * @return \Illuminate\Http\Response */ public function update(Request $request, Food $food) { // } /** * Remove the specified resource from storage. * * @param \App\Models\Food $food * @return \Illuminate\Http\Response */ public function destroy(Food $food) { // } }