【发布时间】:2020-02-10 07:06:04
【问题描述】:
我有这两张表:
产品表:
name - price - quantity - etc
折扣表:
type('percentage','numeric') - value - cancel - expired_at
多态关系:折扣表:
discount_id - discountable_id - discountable_type
它们之间有Many To Many (Polymorphic)。所以我需要得到除了已经有折扣的所有产品+有折扣但已经结束的产品expired_at
产品型号:
public function discounts()
{
return $this->morphToMany('App\Models\Discount', 'discountable');
}
折扣模式:
public function products()
{
return $this->morphedByMany('App\Models\Product', 'discountable')->withTimestamps();
}
我关错了!:
public function index()
{
$products = Product::with('discounts');
return view('cp.discounts.index', compact('products'));
}
【问题讨论】: