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 App\Models\Packing;
use App\Models\Recipe;
use App\Models\Spice;
use App\Rules\SumEquals100;
use Picqer\Barcode\BarcodeGeneratorSVG;
class ProductResource extends Resource {
@@ -50,53 +52,52 @@ class ProductResource extends Resource {
->label('Beschreibung')
->content(fn ($record) => $record?->description ?? '—')
->columnSpanFull(),
Forms\Components\Select::make('recipes_id')
->label('Rezept')
->native(false)
->searchable()
->live()
->relationship('recipe', 'name')
->getOptionLabelFromRecordUsing(fn (Recipe $r) => "{$r->name} (v{$r->version})")
->required()
->createOptionForm(fn () => [
Forms\Components\TextInput::make('name')
Forms\Components\Section::make('Rezept')
->schema([
TextInput::make('recipe_name')
->label('Name')
->required()
->default(fn ($livewire) => $livewire->data['title'] ?? ''),
Forms\Components\Textarea::make('description')
->label('Beschreibung'),
->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()
->required(),
TextInput::make('amount_in_percent')
->label('Anteil (%)')
->numeric()
->minValue(0.01)
->maxValue(100)
->required(),
])
->columns(2)
->addActionLabel('Zutat hinzufügen')
->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()
);
}
}),
])
->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(),
Forms\Components\Textarea::make('description')
->label('Beschreibung'),
]),
Placeholder::make('recipe_summary')
->label('Rezept-Inhalt')
->content(function (Forms\Get $get, $record): HtmlString {
$recipeId = $get('recipes_id');
$recipe = $recipeId ? Recipe::with('spices')->find($recipeId) : $record?->recipe?->load('spices');
if (! $recipe || $recipe->spices->isEmpty()) {
return new HtmlString('<span class="text-sm text-gray-400">Keine Zutaten hinterlegt</span>');
}
$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>");
})
->columnSpanFull()
->visible(fn (Forms\Get $get, $record) => filled($get('recipes_id')) || $record?->recipes_id),
->columns(2)
->columnSpanFull(),
Repeater::make('sizes')
->schema([
Select::make('packing_id')