【发布时间】:2020-02-12 04:31:14
【问题描述】:
我有这些表:
products
-- name
-- price
-- quantity
-- category_id
discounts
-- type('percentage','numeric')
-- value
-- expired_at
categories
-- name
discountables
-- discount_id
-- discountable_id
-- discountable_type
discountables 之间存在多对多关系:
discounts and categories 也 discounts and products
我已经完成了如何在discounts and products 之间打折
现在我很困惑如何对属于我添加到discountables的类别的整个产品进行折扣
关系:
类别模型
public function discounts()
{
return $this->morphToMany('App\Models\Discount', 'discountable');
}
产品型号
public function discounts()
{
return $this->morphToMany('App\Models\Discount', 'discountable');
}
折扣模式:
public function categories()
{
return $this->morphedByMany('App\Models\Category', 'discountable')->withTimestamps();
}
public function products()
{
return $this->morphedByMany('App\Models\Product', 'discountable')->withTimestamps();
}
直接打折商品的MY代码discounts and products
/**
* get price of product after discount
*
* @return void
*/
public function getDiscountProductAttribute() {
foreach ($this->discounts as $discount) {
if($discount->expired_at > Carbon::now()){
if ($discount->type == 'numeric'){
return $this->price - $discount->value;
}else{
return $this->price - ($this->price * ($discount->value / 100));
}
}
}
}
所以我需要如何打折属于我添加到discountables 的类别的整个产品?
【问题讨论】:
-
如果产品有多个类别,并且这些类别中至少有 (2) 个类别有折扣怎么办?在这种情况下,您如何确定分层折扣应用模式?
-
@Ohgodwhy NO 我的电子商务是每个产品只有一个类别
-
如果产品和类别都有折扣?
-
@Ohgodwhy 这正是我的问题!我处理的产品打折,一切都很好,现在我需要一件我不知道如何处理的东西。对除已打折的产品以外的所有产品打折!
-
@Ohgodwhy +随意