ORM whereHas + with ?
return $this->couponPromotion
->where('user_id', $user_id)
->whereHas('coupons', function ($query) use ($country_id) {
$query->where('country_id', $country_id);
})
->with(['coupons' => function ($query) use ($country_id) {
$query->where('country_id', $country_id)
->select([
'id',
'user_id',
'product_title',
]);
}])
->orderBy('id', 'desc')
->paginate($page_size, null, 'page', $page);
从couponPromotion查出了 3条数据,但是有1条数据 coupons 为null .现在只要取coupons不为null的记录, 通过whereHas 过滤了 。能取到了 想要的2条数据。
但是这样写感觉代码怪怪的。请问还有更好的实现办法么。 或者上面代码能怎么优化。
推荐文章: