Files
SpiceManagementSystem/app/Filament/User/Resources/ProductResource/Pages/EditProduct.php
T
Malek TellissiandClaude Sonnet 4.6 e187465553
Deploy / deploy (push) Successful in 14s
Fix syncSizes: replace belongsToMany sync with ProductSize updateOrCreate
belongsToMany sync() is wrong for products_sizes which is a full model
with its own id and a unique shopify_variant_id. The new approach:
- updateOrCreate per row (safe for repeated saves)
- strips non-digits from gtin, stores null when empty (fixes 500 on
  unsignedBigInteger column with empty string in strict SQL mode)
- only deletes local-only sizes (whereNull shopify_variant_id) that
  were removed — Shopify-imported sizes are never deleted here

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

113 lines
3.2 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\Actions;
use Filament\Resources\Pages\EditRecord;
class EditProduct extends EditRecord
{
protected static string $resource = ProductResource::class;
protected function getHeaderActions(): array
{
return [
Actions\Action::make('rezept-versionen')
->label('Rezept-Versionen')
->icon('heroicon-o-clock')
->color('gray')
->url(fn () => ProductResource::getUrl('recipe-history', ['record' => $this->record])),
Actions\DeleteAction::make(),
];
}
protected function afterSave(): void
{
$this->syncSizes();
$this->syncRecipe();
}
protected function syncSizes(): void
{
$product = $this->record;
$incoming = collect($this->data['sizes'] ?? []);
$keepIds = [];
foreach ($incoming as $size) {
if (empty($size['packing_id'])) {
continue;
}
$gtin = preg_replace('/\D/', '', (string) ($size['gtin'] ?? ''));
$ps = ProductSize::updateOrCreate(
[
'product_id' => $product->id,
'packing_id' => (int) $size['packing_id'],
],
[
'price' => $size['price'],
'sku' => $size['sku'] ?: null,
'gtin' => $gtin !== '' ? $gtin : null,
'description' => $size['description'] ?? null,
]
);
$keepIds[] = $ps->id;
}
// Only delete local-only sizes (no shopify_variant_id) that were removed
ProductSize::where('product_id', $product->id)
->whereNotIn('id', $keepIds)
->whereNull('shopify_variant_id')
->delete();
}
protected function syncRecipe(): void
{
$product = $this->record;
$spices = $this->data['recipe_spices'] ?? [];
if (empty($spices)) {
return;
}
$newPivot = collect($spices)->mapWithKeys(fn ($s) => [
(int) $s['spice_id'] => (float) $s['amount_in_percent'],
])->toArray();
$current = $product->recipe;
if ($current) {
$current->load('spices');
$existingPivot = $current->spices->mapWithKeys(fn ($s) => [
$s->id => (float) $s->pivot->amount_in_percent,
])->toArray();
if ($existingPivot == $newPivot) {
return;
}
}
$nextVersion = Recipe::where('product_id', $product->id)->max('version') + 1;
$recipe = Recipe::create([
'name' => $product->title,
'product_id' => $product->id,
'version' => $nextVersion,
]);
$recipe->spices()->sync(
collect($newPivot)->mapWithKeys(fn ($pct, $id) => [
$id => ['amount_in_percent' => $pct],
])->toArray()
);
$product->update(['recipes_id' => $recipe->id]);
}
}