feat: refactor product attributes handling to use product_attribute table and update related models and requests

This commit is contained in:
2026-06-30 16:35:56 -03:00
parent 191a40fcb9
commit cb15905c46
15 changed files with 189 additions and 63 deletions

View File

@@ -36,6 +36,29 @@ return new class extends Migration
$table->unique(['product_id', 'attribute_id']);
});
// 4. Point variant values to the product-attribute pivot instead of the base attribute
Schema::table('productos_variantes_values', function (Blueprint $table) {
$table->dropForeign('productos_variantes_definiciones_attribute_id_foreign');
$table->dropUnique('prod_var_def_variant_attr_unique');
});
Schema::table('productos_variantes_values', function (Blueprint $table) {
$table->renameColumn('attribute_id', 'products_attribute_id');
});
Schema::table('productos_variantes_values', function (Blueprint $table) {
$table->foreign('products_attribute_id')
->references('id')
->on('products_attributes')
->cascadeOnUpdate()
->cascadeOnDelete();
$table->unique(
['producto_variante_id', 'products_attribute_id'],
'prod_var_values_variant_product_attr_unique'
);
});
}
/**
@@ -43,6 +66,25 @@ return new class extends Migration
*/
public function down(): void
{
Schema::table('productos_variantes_values', function (Blueprint $table) {
$table->dropForeign(['products_attribute_id']);
$table->dropUnique('prod_var_values_variant_product_attr_unique');
});
Schema::table('productos_variantes_values', function (Blueprint $table) {
$table->renameColumn('products_attribute_id', 'attribute_id');
});
Schema::table('productos_variantes_values', function (Blueprint $table) {
$table->foreign('attribute_id')
->references('id')
->on('attribute')
->cascadeOnUpdate()
->cascadeOnDelete();
$table->unique(['producto_variante_id', 'attribute_id'], 'prod_var_def_variant_attr_unique');
});
Schema::dropIfExists('products_attributes');
Schema::rename('productos_variantes_values', 'productos_variantes_definiciones');
Schema::rename('attribute', 'productos_attributes');

View File

@@ -10,6 +10,7 @@ use App\Domains\Catalog\Services\ProductService;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Database\Seeder;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use RuntimeException;
@@ -205,6 +206,10 @@ class ProductCatalogFromImagesSeeder extends Seeder
'attribute_ids' => array_values($attributeIds->all()),
]);
$productAttributeIds = DB::table('products_attributes')
->where('product_id', $product->id)
->pluck('id', 'attribute_id');
$sizes = $metadata['type'] === 'zapatillas'
? self::SHOE_SIZES
: self::CLOTHING_SIZES;
@@ -219,8 +224,14 @@ class ProductCatalogFromImagesSeeder extends Seeder
$definitions = [];
if (isset($attributeIds['color']) && $variantGroup['metadata']['color'] !== null) {
$colorProductAttributeId = $productAttributeIds[$attributeIds['color']] ?? null;
if ($colorProductAttributeId === null) {
throw new RuntimeException("Missing product attribute for color on product '{$metadata['product_slug']}'.");
}
$definitions[] = [
'attribute_id' => $attributeIds['color'],
'products_attribute_id' => $colorProductAttributeId,
'value' => $variantGroup['metadata']['color'],
];
}
@@ -229,8 +240,14 @@ class ProductCatalogFromImagesSeeder extends Seeder
? 'talle_numerico'
: 'talle';
$sizeProductAttributeId = $productAttributeIds[$attributeIds[$sizeAttributeCode]] ?? null;
if ($sizeProductAttributeId === null) {
throw new RuntimeException("Missing product attribute for size on product '{$metadata['product_slug']}'.");
}
$definitions[] = [
'attribute_id' => $attributeIds[$sizeAttributeCode],
'products_attribute_id' => $sizeProductAttributeId,
'value' => $size,
];