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
+31
-14
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Filament\User\Resources\ProductResource\Pages;
|
namespace App\Filament\User\Resources\ProductResource\Pages;
|
||||||
|
|
||||||
use App\Filament\User\Resources\ProductResource;
|
use App\Filament\User\Resources\ProductResource;
|
||||||
|
use App\Models\ProductSize;
|
||||||
use App\Models\Recipe;
|
use App\Models\Recipe;
|
||||||
use Filament\Actions;
|
use Filament\Actions;
|
||||||
use Filament\Resources\Pages\EditRecord;
|
use Filament\Resources\Pages\EditRecord;
|
||||||
@@ -31,22 +32,38 @@ class EditProduct extends EditRecord
|
|||||||
|
|
||||||
protected function syncSizes(): void
|
protected function syncSizes(): void
|
||||||
{
|
{
|
||||||
$product = $this->record;
|
$product = $this->record;
|
||||||
|
$incoming = collect($this->data['sizes'] ?? []);
|
||||||
|
$keepIds = [];
|
||||||
|
|
||||||
$sizes = collect($this->data['sizes'] ?? [])
|
foreach ($incoming as $size) {
|
||||||
->mapWithKeys(function ($size) {
|
if (empty($size['packing_id'])) {
|
||||||
return [
|
continue;
|
||||||
$size['packing_id'] => [
|
}
|
||||||
'description' => $size['description'] ?? null,
|
|
||||||
'price' => $size['price'],
|
|
||||||
'sku' => $size['sku'] ?? null,
|
|
||||||
'gtin' => $size['gtin'] ?? null,
|
|
||||||
],
|
|
||||||
];
|
|
||||||
})
|
|
||||||
->toArray();
|
|
||||||
|
|
||||||
$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
|
protected function syncRecipe(): void
|
||||||
|
|||||||
Reference in new issue
Block a user