我有這兩張桌子:產品表: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'));}
1 回答

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
您可以使用“沒有”方法,
以下查詢將檢索沒有任何折扣的所有產品。
Product::doesntHave('discounts')->get();
如果您想獲得產品,除了它結束了+沒有任何折扣,您可以使用在關閉中添加條件:expired_atwhereDoesntHave
Product::whereDoesntHave('discounts', function($query) {
$query->where('expired_at', '>', \Carbon\Carbon::now()->format('Y-m-d H:i:s'));
})->get();
- 1 回答
- 0 關注
- 100 瀏覽
添加回答
舉報
0/150
提交
取消