mirror of https://github.com/kcal-app/kcal.git
Add step support to recipe create form
This commit is contained in:
parent
f8c6f81e60
commit
198a32a915
|
@ -5,6 +5,8 @@ namespace App\Http\Controllers;
|
|||
use App\Models\Ingredient;
|
||||
use App\Models\IngredientAmount;
|
||||
use App\Models\Recipe;
|
||||
use App\Models\RecipeStep;
|
||||
use App\Rules\ArrayNotEmpty;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
@ -43,7 +45,7 @@ class RecipeController extends Controller
|
|||
['value' => 'tsp', 'label' => 'tsp.'],
|
||||
['value' => 'tbsp', 'label' => 'tbsp.'],
|
||||
['value' => 'cup', 'label' => 'cup'],
|
||||
['value' => 'g', 'label' => 'g'],
|
||||
['value' => 'grams', 'label' => 'g'],
|
||||
]));
|
||||
}
|
||||
|
||||
|
@ -59,12 +61,14 @@ class RecipeController extends Controller
|
|||
'name' => 'required|string',
|
||||
'description' => 'required|string',
|
||||
'servings' => 'required|numeric',
|
||||
'ingredients_amount' => 'required|array',
|
||||
'ingredients_amount' => ['required', 'array', new ArrayNotEmpty],
|
||||
'ingredients_amount.*' => 'required_with:ingredients.*|nullable|numeric|min:0',
|
||||
'ingredients_unit' => 'required|array',
|
||||
'ingredients_unit' => ['required', 'array', new ArrayNotEmpty],
|
||||
'ingredients_unit.*' => 'nullable|string',
|
||||
'ingredients' => 'required|array',
|
||||
'ingredients' => ['required', 'array', new ArrayNotEmpty],
|
||||
'ingredients.*' => 'required_with:ingredients_amount.*|nullable|exists:App\Models\Ingredient,id',
|
||||
'steps' => ['required', 'array', new ArrayNotEmpty],
|
||||
'steps.*' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$recipe = new Recipe([
|
||||
|
@ -78,17 +82,28 @@ class RecipeController extends Controller
|
|||
if (!$recipe->save()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$ingredient_amounts = [];
|
||||
$weight = 0;
|
||||
foreach (array_filter($input['ingredients_amount']) as $key => $amount) {
|
||||
$ingredient_amounts[$key] = new IngredientAmount([
|
||||
'amount' => (float) $amount,
|
||||
'unit' => $input['ingredients_unit'][$key],
|
||||
'weight' => (int) $key,
|
||||
'weight' => $weight++,
|
||||
]);
|
||||
$ingredient_amounts[$key]->recipe()->associate($recipe);
|
||||
$ingredient_amounts[$key]->ingredient()->associate($input['ingredients'][$key]);
|
||||
}
|
||||
$recipe->ingredientAmounts()->saveMany($ingredient_amounts);
|
||||
|
||||
$steps = [];
|
||||
$number = 1;
|
||||
foreach (array_filter($input['steps']) as $step) {
|
||||
$steps[] = new RecipeStep([
|
||||
'number' => $number++,
|
||||
'step' => $step,
|
||||
]);
|
||||
}
|
||||
$recipe->ingredientAmounts()->saveMany($steps);
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class ArrayNotEmpty implements Rule
|
||||
{
|
||||
/**
|
||||
* Determine if the array is empty.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
return !empty(array_filter($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
return 'At least one :attribute must be set.';
|
||||
}
|
||||
}
|
|
@ -66,7 +66,7 @@
|
|||
|
||||
<!-- Ingredients -->
|
||||
<h3 class="pt-2 mb-2 font-extrabold">Ingredients</h3>
|
||||
@for($i = 0; $i < 5; $i++)
|
||||
@for($i = 0; $i < 10; $i++)
|
||||
<div class="flex flex-row space-x-4 mb-4">
|
||||
<x-inputs.input type="number"
|
||||
name="ingredients_amount[]"
|
||||
|
@ -87,12 +87,12 @@
|
|||
|
||||
<!-- Steps -->
|
||||
<h3 class="pt-2 mb-2 font-extrabold">Steps</h3>
|
||||
@for($i = 1; $i < 6; $i++)
|
||||
@for($i = 0; $i < 10; $i++)
|
||||
<div class="flex flex-row space-x-4 mb-4">
|
||||
<div class="text-3xl text-gray-400 text-center">{{ $i }}</div>
|
||||
<div class="text-3xl text-gray-400 text-center">{{ $i + 1 }}</div>
|
||||
<x-inputs.textarea class="block mt-1 w-full"
|
||||
name="steps[]"
|
||||
:value="old('steps[$i]')" />
|
||||
:value="old('steps.' . $i)" />
|
||||
</div>
|
||||
@endfor
|
||||
|
||||
|
|
Loading…
Reference in New Issue