MYSQL Laravel 产品变体结构

时间:2021-02-01 10:16:56

标签: php mysql

我正在开发一个新的电子商务平台即服务,但我在产品变体数据库方面遇到了一些困难。 所以我基本上想构建类似 Shopify 的平台,而且我还在数据库设计上,这就是我所做的。

Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->longText('description');
            $table->set('type', ['Single Product', 'Variation']);
            $table->unsignedBigInteger('price_id')->nullable();
            $table->unsignedBigInteger('category_id');
            $table->timestamps();
        });
        
        Schema::create('product_prices', function (Blueprint $table) {
            $table->id();
            $table->float('real_price', 8, 2);
            $table->float('sale_price', 8, 2)->nullable();
            $table->float('delivery_price', 8, 2);
            $table->timestamps();
        });

        Schema::create('variations_attributes', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('variations_attributes_names', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('attribute_id');
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('product_variations', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id');
            $table->unsignedBigInteger('price_id');
            $table->string('image');
            $table->string('variation_name');
            $table->string('sku_id');
            $table->string('variation_ids'); // 123/Color, 321/Size, etc...
            $table->timestamps();
        });

        Schema::create('products_sku', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id');
            $table->unsignedBigInteger('product_variation')->nullable();
            $table->integer('sku')->unique();
            $table->integer('qty');
            $table->timestamps();
        });

(这还是个未完成的数据库,以后还会有更多的,外键也没有正确形成,但你还是明白了)

那么现在,我对结构变体的想法是什么。 客户将在前端有选项,并从variations_attributes(尺寸、颜色、发货来源等)中选择他喜欢的数量,这取决于他在那里添加的内容,在variations_attributes_names 中将是(红色、绿色、M、L、XXL,中国、俄罗斯等……)。 就像在 Shopify 表单中一样,当他选择属性并添加属性名称时,它会为他创建一个表单(例如,他选择了所有 3 个属性,颜色为红色和绿色、尺寸 M 和 L,以及来自俄罗斯和中国的船舶)表单看起来像:

RED/M/China - price (custom price) and sku
RED/M/Russia - price (custom price) and sku
RED/L/China - price (custom price) and sku
RED/L/Russia - price (custom price) and sku
GREEN/M/China - price (custom price) and sku
GREEN/M/Russia - price (custom price) and sku
GREEN/L/China - price (custom price) and sku
GREEN/L/Russia - price (custom price) and sku

所以基本上它会多重选择他选择的所有选项,并让他选择为所有变体添加自定义价格。

因此,在这种情况下,我对 product_variations 数据库的字段variation_ids 的想法是获取所选属性的 id 并将它们存储为

1(attribute id)\1(attribute name id);1(second attribute id)\1(second attribute name id);1(third attribute id)\1(third attribute name id)

所以在这种情况下,当他选择前端属性值时,它会选择他想要的值。但是在那种情况下,如果他选择第一个尺寸而不是颜色,则会出现错误,这将是一个问题。

知道如何构建该数据库吗?

1 个答案:

答案 0 :(得分:0)

我会使用 Json column 表示具有以下结构的“变体”:

[
  [ 'attribute_id' => 1, 'attribute_name_id' => 1],
  [ 'attribute_id' => 2, 'attribute_name_id' => 2],
  [ 'attribute_id' => 3, 'attribute_name_id' => 3],
]

通过这种方式,您可以轻松访问这些属性的列表,并且在您需要向变体中添加其他字段时非常灵活。

相关问题