简体   繁体   中英

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:

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 ). 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).

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.

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_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:

  • 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. 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:

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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