Fix syncSizes: replace belongsToMany sync with ProductSize updateOrCreate
Deploy / deploy (push) Successful in 14s

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>
This commit is contained in:
Malek TellissiandClaude Sonnet 4.6 committed 2026-09-12 17:00:58 +02:00
1 parent 538053d36e
commit e187465553
1 file changed
+31 -14
@@ -3,6 +3,7 @@
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;
@@ -31,22 +32,38 @@ class EditProduct extends EditRecord
protected function syncSizes(): void
{
$product = $this->record;
$product = $this->record;
$incoming = collect($this->data['sizes'] ?? []);
$keepIds = [];
$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();
foreach ($incoming as $size) {
if (empty($size['packing_id'])) {
continue;
}
$product->sizes()->sync($sizes);
$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