Files
SpiceManagementSystem/app/Filament/User/Resources/ProductResource/Pages/CreateProduct.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

51 lines
1.5 KiB
PHP

<?php
namespace App\Filament\User\Resources\ProductResource\Pages;
use App\Filament\User\Resources\ProductResource;
use App\Models\ProductSize;
use App\Models\Recipe;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model;
class CreateProduct extends CreateRecord
{
protected static string $resource = ProductResource::class;
protected function handleRecordCreation(array $data): Model
{
$record = static::getModel()::create($data);
foreach ($data['sizes'] ?? [] as $size) {
ProductSize::create([
'product_id' => $record->id,
'packing_id' => $size['packing_id'],
'description' => $size['description'] ?? null,
'price' => $size['price'],
'sku' => $size['sku'] ?? 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;
}
}