diff --git a/app/Http/Controllers/IngredientController.php b/app/Http/Controllers/IngredientController.php index 84969bc..9b6e9da 100644 --- a/app/Http/Controllers/IngredientController.php +++ b/app/Http/Controllers/IngredientController.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers; use App\Models\Ingredient; +use Illuminate\Contracts\View\View; +use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; class IngredientController extends Controller @@ -20,22 +22,33 @@ class IngredientController extends Controller /** * Show the form for creating a new resource. * - * @return \Illuminate\Http\Response + * @return \Illuminate\Contracts\View\View */ - public function create() + public function create(): View { - // + return view('ingredients.create'); } /** * Store a newly created resource in storage. - * - * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(Request $request): RedirectResponse { - // + $attributes = $request->validate([ + 'name' => 'required|string', + 'detail' => 'nullable|string', + 'carbohydrates' => 'nullable|numeric', + 'calories' => 'nullable|numeric', + 'cholesterol' => 'nullable|numeric', + 'fat' => 'nullable|numeric', + 'protein' => 'nullable|numeric', + 'sodium' => 'nullable|numeric', + 'unit_weight' => 'required_without:cup_weight|nullable|numeric', + 'cup_weight' => 'required_without:unit_weight|nullable|numeric', + ]); + /** @var \App\Models\Ingredient $ingredient */ + $ingredient = tap(new Ingredient(array_filter($attributes)))->save(); + return redirect()->back()->with('message', "Ingredient {$ingredient->name} added!"); } /** diff --git a/app/Models/Ingredient.php b/app/Models/Ingredient.php index 5a58d59..837c9da 100644 --- a/app/Models/Ingredient.php +++ b/app/Models/Ingredient.php @@ -36,8 +36,8 @@ class Ingredient extends Model 'fat', 'protein', 'sodium', - 'unitWeight', - 'cupWeight', + 'unit_weight', + 'cup_weight', ]; /** diff --git a/resources/views/ingredients/create.blade.php b/resources/views/ingredients/create.blade.php new file mode 100644 index 0000000..77596da --- /dev/null +++ b/resources/views/ingredients/create.blade.php @@ -0,0 +1,178 @@ + + +

+ {{ __('Add Ingredient') }} +

+
+ +
+
+ @if(session()->has('message')) +
+ {{ session()->get('message') }} +
+ @endif + + @if ($errors->any()) +
+ @foreach ($errors->all() as $error) +
+ {{ $error }} +
+ @endforeach +
+ @endif + +
+
+
+ @csrf +
+
+ +
+ + + +
+ + +
+ + + +
+
+ +
+ +
+ + + +
+ + +
+ + + +
+ + +
+ + + +
+ + +
+ + + +
+ + +
+ + + +
+ + +
+ + + +
+
+ +
+ +
+ + + +
+ +
+ or +
+ + +
+ + + +
+
+
+
+ + {{ __('Add') }} + +
+
+
+
+
+
+
diff --git a/resources/views/layouts/navigation.blade.php b/resources/views/layouts/navigation.blade.php index e7e40a7..89c9645 100644 --- a/resources/views/layouts/navigation.blade.php +++ b/resources/views/layouts/navigation.blade.php @@ -16,6 +16,11 @@ {{ __('Dashboard') }} + diff --git a/routes/web.php b/routes/web.php index 852b11f..ec441d9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ middleware(['auth'])->name('dashboard'); +Route::resource('ingredients', IngredientController::class); + require __DIR__.'/auth.php';