Files
SpiceManagementSystem/app/Filament/User/Resources/ProductResource/Pages/EditProduct.php
T
Malek TellissiandClaude Sonnet 4.6 4775cffaae
Deploy / deploy (push) Successful in 14s
Embed recipe editing directly on product page
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>
2026-09-12 05:43:37 +02:00

74 lines
2.1 KiB
PHP

<?php
namespace App\Filament\User\Resources\ProductResource\Pages;
use App\Filament\User\Resources\ProductResource;
use App\Models\Recipe;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditProduct extends EditRecord
{
protected static string $resource = ProductResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
protected function afterSave(): void {
$this->syncSizes();
$this->syncRecipe();
}
protected function syncSizes(): void {
$product = $this->record;
$sizes = collect($this->data['sizes'] ?? [])
->mapWithKeys(function ($size) {
return [
$size['packing_id'] => [
'description' => $size['description'] ?? null,
'price' => $size['price'],
'sku' => $size['sku'] ?? null,
'gtin' => $size['gtin'] ?? null,
],
];
})
->toArray();
$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]);
}
}
}