Fix syncSizes: replace belongsToMany sync with ProductSize updateOrCreate
Deploy / deploy (push) Successful in 14s
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:
1 parent
538053d36e
commit
e187465553
1 file changed
+29
-12
@@ -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;
|
||||
@@ -32,21 +33,37 @@ class EditProduct extends EditRecord
|
||||
protected function syncSizes(): void
|
||||
{
|
||||
$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,
|
||||
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'],
|
||||
],
|
||||
];
|
||||
})
|
||||
->toArray();
|
||||
[
|
||||
'price' => $size['price'],
|
||||
'sku' => $size['sku'] ?: null,
|
||||
'gtin' => $gtin !== '' ? $gtin : null,
|
||||
'description' => $size['description'] ?? null,
|
||||
]
|
||||
);
|
||||
|
||||
$product->sizes()->sync($sizes);
|
||||
$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
|
||||
|
||||
Reference in new issue
Block a user