Laravel
그룹별로 카운팅하고 싶을 때 + 그룹핑 대상에 where처럼 조건 걸고싶을 때 (GroupBy + Having)
짱구를왜말려?
2022. 1. 21. 10:50
반응형
SMALL
# What?
데이터를 그룹별로 정리하고, 해당 그룹들에 조건 걸고 싶을 때
(날짜별로 데이터 정리 후, 각 날짜별 그룹중 어떤 조건을 만족하는 데이터들만 추출하기)
# How?
$days = $dietProduct->products()
->groupBy("product_product.assignment_at")
->select("product_product.assignment_at", DB::raw('count(*) as total'))
->having("total", "<", 4)
->get();
LIST