Create Ingredient create form

This commit is contained in:
Christopher C. Wells 2020-12-23 11:47:49 -08:00
parent cccc2d2760
commit 9026ee9240
5 changed files with 209 additions and 10 deletions

View File

@ -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!");
}
/**

View File

@ -36,8 +36,8 @@ class Ingredient extends Model
'fat',
'protein',
'sodium',
'unitWeight',
'cupWeight',
'unit_weight',
'cup_weight',
];
/**

View File

@ -0,0 +1,178 @@
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Add Ingredient') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
@if(session()->has('message'))
<div class="bg-green-200 border-2 border-green-600 p-2 mb-2">
{{ session()->get('message') }}
</div>
@endif
@if ($errors->any())
<div class="flex flex-col space-y-2 pb-2">
@foreach ($errors->all() as $error)
<div class="bg-red-200 border-2 border-red-900 p-2">
{{ $error }}
</div>
@endforeach
</div>
@endif
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
<form method="POST" action="{{ route('ingredients.store') }}">
@csrf
<div class="flex flex-col space-y-4">
<div class="grid grid-cols-2 gap-4">
<!-- Name -->
<div>
<x-label for="name" :value="__('Name')" />
<x-input id="name"
class="block mt-1 w-full"
type="text"
name="name"
:value="old('name')"
required />
</div>
<!-- Detail -->
<div>
<x-label for="detail" :value="__('Detail')" />
<x-input id="detail"
class="block mt-1 w-full"
type="text"
name="detail"
:value="old('detail')" />
</div>
</div>
<div class="grid grid-rows-3 md:grid-rows-2 lg:grid-rows-1 grid-flow-col">
<!-- Calories -->
<div>
<x-label for="calories" :value="__('Calories')" />
<x-input id="calories"
class="block mt-1"
type="number"
step="any"
name="calories"
size="10"
:value="old('calories')" />
</div>
<!-- Carbohydrates -->
<div>
<x-label for="carbohydrates" :value="__('Carbohydrates')" />
<x-input id="carbohydrates"
class="block mt-1"
type="number"
step="any"
name="carbohydrates"
size="10"
:value="old('carbohydrates')" />
</div>
<!-- Cholesterol -->
<div>
<x-label for="cholesterol" :value="__('Cholesterol')" />
<x-input id="cholesterol"
class="block mt-1"
type="number"
step="any"
name="cholesterol"
size="10"
:value="old('cholesterol')" />
</div>
<!-- Fat -->
<div>
<x-label for="fat" :value="__('Fat')" />
<x-input id="fat"
class="block mt-1"
type="number"
step="any"
name="fat"
size="10"
:value="old('fat')" />
</div>
<!-- Protein -->
<div>
<x-label for="protein" :value="__('Protein')" />
<x-input id="protein"
class="block mt-1"
type="number"
step="any"
name="protein"
size="10"
:value="old('protein')" />
</div>
<!-- Sodium -->
<div>
<x-label for="sodium" :value="__('Sodium')" />
<x-input id="sodium"
class="block mt-1"
type="number"
step="any"
name="sodium"
size="10"
:value="old('sodium')" />
</div>
</div>
<div class="flex items-center">
<!-- Cup weight -->
<div>
<x-label for="cup_weight" :value="__('Cup weight')" />
<x-input id="cup_weight"
class="block mt-1"
type="number"
step="any"
name="cup_weight"
size="10"
:value="old('cup_weight')" />
</div>
<div class="p-4 font-black text-3xl">
or
</div>
<!-- Unit weight -->
<div>
<x-label for="unit_weight" :value="__('Unit weight')" />
<x-input id="unit_weight"
class="block mt-1"
type="number"
step="any"
name="unit_weight"
size="10"
:value="old('unit_weight')" />
</div>
</div>
</div>
<div class="flex items-center justify-end mt-4">
<x-button class="ml-3">
{{ __('Add') }}
</x-button>
</div>
</form>
</div>
</div>
</div>
</div>
</x-app-layout>

View File

@ -16,6 +16,11 @@
{{ __('Dashboard') }}
</x-nav-link>
</div>
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<x-nav-link :href="route('ingredients.create')" :active="request()->routeIs('ingredients.create')">
{{ __('Add Ingredient') }}
</x-nav-link>
</div>
</div>
<!-- Settings Dropdown -->

View File

@ -1,5 +1,6 @@
<?php
use App\Http\Controllers\IngredientController;
use Illuminate\Support\Facades\Route;
/*
@ -21,4 +22,6 @@ Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Route::resource('ingredients', IngredientController::class);
require __DIR__.'/auth.php';