Files
SpiceManagementSystem/app/Filament/User/Resources/ProductResource/Pages/CreateProduct.php
T
Malek TellissiandClaude Sonnet 4.6 269570c7d6
Deploy / deploy (push) Successful in 15s
Remove recipe name field; use product title internally
The recipe name input is dropped from the product form — the version
number alone identifies a recipe. The product title is stored as the
internal name to satisfy the NOT NULL constraint. The history table
no longer shows the name column.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-12 06:00:18 +02:00

50 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,
]);
}
$spices = $data['recipe_spices'] ?? [];
if (! empty($spices)) {
$recipe = Recipe::create([
'name' => $data['title'],
'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;
}
}