简体   繁体   English

我该如何处理 Coupon 数据库设计?

[英]How can I deal with Coupon database design?

I have create a simple e-commerce via Laravel, and have some questions about COUPON database design, what I need as follows:我已经通过 Laravel 创建了一个简单的电子商务,并且对 COUPON 数据库设计有一些疑问,我需要的内容如下:

1- I need a normal coupon for checkout (when the user adds all products that he needs then put the coupon this will discount from the total price ). 1-我需要一张普通的结帐优惠券(当用户添加他需要的所有产品然后放入优惠券时,这将从总价中扣除)。 I'm done with this:我完成了这个:

 Schema::create('coupons', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('code');
        $table->enum('type', ['percentage', 'numeric']);
        $table->integer('value');
        $table->integer('count')->nullable();
        $table->date('expired_at');
        $table->timestamps();
  });

2- I need to put a discount on products of a single category (to put a discount on the whole products that belong to the T-Shirt category). 2- 我需要对单个类别的产品打折(对属于 T 恤类别的整个产品打折)。

How the design structure of this point?

3- I need to put a discount on a specific product or products that I want to put on it. 3- 我需要对我想要的特定产品或产品打折。

How the design structure of this point?

Could anyone pls help me with this?任何人都可以帮我解决这个问题吗? So confused!如此迷茫!

Update:更新:

products table:产品表:

id - name - price - quantity - category_id - brand_id - created_at

categories table类别表

 id - category_name - created_at

Orders table订单表

id - status - user_id - address_id - coupon_id - created_at

order_product pivot table order_product 数据透视表

order_id - product_id - quantity

Very interesting!很有意思! I think may be a good approach should be a pivot table with a polymorphic relation.我认为可能是一个很好的方法应该是一个具有多态关系的数据透视表。 Somthing like this:像这样的东西:

id - couponable_type - couponable_id - coupon_id - "whatever_data"
------------------------------------------------------------------
1  - category        - 1             - 1         - ....
2  - order           - 1             - 2         - ....
3  - product         - 1             - 1         - ....

Since the COUPON ** has a relation by default with **PRODUCTS , it will be great to make some relationship, like so:由于COUPON ** 默认与 **PRODUCTS有关系,因此建立一些关系会很棒,如下所示:

  • Does the product will have one coupon or many?产品会有一张优惠券还是多张优惠券
  • Does the coupon will belong to one product or many?优惠券是属于一种产品还是属于多种产品

I think it's good to give product ability to use many **coupons and the coupon FOR SURE will belong to many products.我认为给产品能力使用很多**优惠券是好的,而且优惠券肯定会属于很多产品。 In that case, you should add Many To Many Relationship between coupons and products.在这种情况下,您应该在优惠券和产品之间添加多对多关系。

The same thing with products and categories , the product should have many categories and the later should have or belong to many **products.产品类别也是一样,产品应该有很多类别,后面应该有或属于很多**产品。

If you go with that approach you can achieve the Points 01 and 02 easily.如果您采用这种方法,您可以轻松实现第01点和第02点。 You can then create coupons then attach them to specific product or products, or a product category.然后,您可以创建优惠券,然后将它们附加到特定的产品或产品或产品类别。

To list the coupons of a product or category, you can do something like this:要列出产品或类别的优惠券,您可以执行以下操作:

$product_coupons = Product::findOrFail($id)->coupons;
$category_coupons = ProductCategory::findOrFail($id)->coupons;

In Blade you access the coupons like this:在 Blade 中,您可以像这样访问优惠券:

@foreach($product->coupons as $coupon)
{{ $coupon->code }}
@endforeach

But @Nahuelsgk approach is a good one, advanced but it's clean, I think it's better to use polymorphic但是@Nahuelsgk 方法是一个很好的方法,先进但很干净,我认为最好使用多态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM