Embed recipe editing directly on product page
Deploy / deploy (push) Successful in 14s

Replaces the recipe dropdown with an inline Section containing a name
field and a spice-repeater with percentage inputs. On save, the recipe
is created (version 1) or updated in place; spice pivot rows are synced
via sync(). SumEquals100 rule enforces that percentages add up to 100.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Malek TellissiandClaude Sonnet 4.6 committed 2026-09-12 05:43:37 +02:00
1 parent af55857883
commit 4775cffaae
3 files changed
+100 -48

No files matched your search

+45 -44
View File
@@ -19,6 +19,8 @@ use Filament\Tables\Table;
use Illuminate\Support\HtmlString; use Illuminate\Support\HtmlString;
use App\Models\Packing; use App\Models\Packing;
use App\Models\Recipe; use App\Models\Recipe;
use App\Models\Spice;
use App\Rules\SumEquals100;
use Picqer\Barcode\BarcodeGeneratorSVG; use Picqer\Barcode\BarcodeGeneratorSVG;
class ProductResource extends Resource { class ProductResource extends Resource {
@@ -50,53 +52,52 @@ class ProductResource extends Resource {
->label('Beschreibung') ->label('Beschreibung')
->content(fn ($record) => $record?->description ?? '—') ->content(fn ($record) => $record?->description ?? '—')
->columnSpanFull(), ->columnSpanFull(),
Forms\Components\Select::make('recipes_id') Forms\Components\Section::make('Rezept')
->label('Rezept') ->schema([
->native(false) TextInput::make('recipe_name')
->label('Name')
->required()
->dehydrated(false)
->afterStateHydrated(fn ($component, $record) =>
$component->state($record?->recipe?->name ?? '')),
Placeholder::make('recipe_version')
->label('Version')
->content(fn ($record) => $record?->recipe
? 'v' . $record->recipe->version . ' · ' . $record->recipe->created_at->format('d.m.Y')
: '—'),
Repeater::make('recipe_spices')
->label('Zutaten')
->schema([
Select::make('spice_id')
->label('Rohstoff')
->options(Spice::all()->pluck('name', 'id'))
->searchable() ->searchable()
->live()
->relationship('recipe', 'name')
->getOptionLabelFromRecordUsing(fn (Recipe $r) => "{$r->name} (v{$r->version})")
->required()
->createOptionForm(fn () => [
Forms\Components\TextInput::make('name')
->label('Name')
->required()
->default(fn ($livewire) => $livewire->data['title'] ?? ''),
Forms\Components\Textarea::make('description')
->label('Beschreibung'),
])
->createOptionUsing(function (array $data, $livewire) {
$productId = $livewire->record?->id;
$nextVersion = Recipe::where('product_id', $productId)->max('version') + 1;
return Recipe::create([
'name' => $data['name'],
'description' => $data['description'] ?? null,
'product_id' => $productId,
'version' => $nextVersion,
])->getKey();
})
->editOptionForm([
Forms\Components\TextInput::make('name')
->label('Name')
->required(), ->required(),
Forms\Components\Textarea::make('description') TextInput::make('amount_in_percent')
->label('Beschreibung'), ->label('Anteil (%)')
]), ->numeric()
Placeholder::make('recipe_summary') ->minValue(0.01)
->label('Rezept-Inhalt') ->maxValue(100)
->content(function (Forms\Get $get, $record): HtmlString { ->required(),
$recipeId = $get('recipes_id'); ])
$recipe = $recipeId ? Recipe::with('spices')->find($recipeId) : $record?->recipe?->load('spices'); ->columns(2)
if (! $recipe || $recipe->spices->isEmpty()) { ->addActionLabel('Zutat hinzufügen')
return new HtmlString('<span class="text-sm text-gray-400">Keine Zutaten hinterlegt</span>'); ->dehydrated(false)
->rules([new SumEquals100()])
->afterStateHydrated(function ($component, $record) {
if ($record?->recipe) {
$record->recipe->load('spices');
$component->state(
$record->recipe->spices->map(fn ($s) => [
'spice_id' => $s->id,
'amount_in_percent' => $s->pivot->amount_in_percent,
])->toArray()
);
} }
$items = $recipe->spices->map(fn ($s) => "<li>{$s->name} ({$s->pivot->amount_in_percent}%)</li>")->implode(''); }),
$meta = "v{$recipe->version} · erstellt " . $recipe->created_at->format('d.m.Y'); ])
return new HtmlString("<p class='text-xs text-gray-400 mb-1'>{$meta}</p><ul class='list-disc list-inside text-sm'>{$items}</ul>"); ->columns(2)
}) ->columnSpanFull(),
->columnSpanFull()
->visible(fn (Forms\Get $get, $record) => filled($get('recipes_id')) || $record?->recipes_id),
Repeater::make('sizes') Repeater::make('sizes')
->schema([ ->schema([
Select::make('packing_id') Select::make('packing_id')
@@ -3,19 +3,20 @@
namespace App\Filament\User\Resources\ProductResource\Pages; namespace App\Filament\User\Resources\ProductResource\Pages;
use App\Filament\User\Resources\ProductResource; use App\Filament\User\Resources\ProductResource;
use App\Models\ProductSize;
use App\Models\Recipe;
use Filament\Resources\Pages\CreateRecord; use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use App\Models\ProductSize;
class CreateProduct extends CreateRecord class CreateProduct extends CreateRecord
{ {
protected static string $resource = ProductResource::class; protected static string $resource = ProductResource::class;
protected function handleRecordCreation(array $data): Model { protected function handleRecordCreation(array $data): Model
{
$record = static::getModel()::create($data); $record = static::getModel()::create($data);
foreach ($data['sizes'] as $size) { foreach ($data['sizes'] ?? [] as $size) {
ProductSize::create([ ProductSize::create([
'product_id' => $record->id, 'product_id' => $record->id,
'packing_id' => $size['packing_id'], 'packing_id' => $size['packing_id'],
@@ -25,6 +26,25 @@ class CreateProduct extends CreateRecord
'gtin' => $size['gtin'] ?? null, 'gtin' => $size['gtin'] ?? null,
]); ]);
} }
$recipeName = $data['recipe_name'] ?? null;
$spices = $data['recipe_spices'] ?? [];
if ($recipeName) {
$recipe = Recipe::create([
'name' => $recipeName,
'product_id' => $record->id,
'version' => 1,
]);
$pivotData = collect($spices)->mapWithKeys(fn ($s) => [
$s['spice_id'] => ['amount_in_percent' => $s['amount_in_percent']],
])->toArray();
$recipe->spices()->sync($pivotData);
$record->update(['recipes_id' => $recipe->id]);
}
return $record; return $record;
} }
} }
@@ -3,6 +3,7 @@
namespace App\Filament\User\Resources\ProductResource\Pages; namespace App\Filament\User\Resources\ProductResource\Pages;
use App\Filament\User\Resources\ProductResource; use App\Filament\User\Resources\ProductResource;
use App\Models\Recipe;
use Filament\Actions; use Filament\Actions;
use Filament\Resources\Pages\EditRecord; use Filament\Resources\Pages\EditRecord;
@@ -19,6 +20,7 @@ class EditProduct extends EditRecord
protected function afterSave(): void { protected function afterSave(): void {
$this->syncSizes(); $this->syncSizes();
$this->syncRecipe();
} }
protected function syncSizes(): void { protected function syncSizes(): void {
@@ -39,4 +41,33 @@ class EditProduct extends EditRecord
$product->sizes()->sync($sizes); $product->sizes()->sync($sizes);
} }
protected function syncRecipe(): void
{
$product = $this->record;
$name = $this->data['recipe_name'] ?? null;
$spices = $this->data['recipe_spices'] ?? [];
if (! $name) {
return;
}
$pivotData = collect($spices)->mapWithKeys(fn ($s) => [
$s['spice_id'] => ['amount_in_percent' => $s['amount_in_percent']],
])->toArray();
if ($product->recipe) {
$product->recipe->update(['name' => $name]);
$product->recipe->spices()->sync($pivotData);
} else {
$nextVersion = Recipe::where('product_id', $product->id)->max('version') + 1;
$recipe = Recipe::create([
'name' => $name,
'product_id' => $product->id,
'version' => $nextVersion,
]);
$recipe->spices()->sync($pivotData);
$product->update(['recipes_id' => $recipe->id]);
}
}
} }