请问 Laravel 怎样对关联关系进行统计查询?
大佬们 ,请问有如下两个一对多关系:user (客户) 和 cars (购车记录)
cars 有 税后价格 字段
user
id
cars
id
user_id
after_tax_price
created_at
我用如下查询 查每个 客户的消费记录
User::query()
->withCount([
'car as total_consumption' => function ($query) use ($params) {
$query->select(DB::raw("SUM(after_tax_price) as total_consumption"))
->whereBetween('created_at',
[
$params['purchase_date_start'], $params['purchase_date_end']
])},
])>orderBy('id');
现在问题来了:请问怎样筛选出 消费额 total_consumption 大于20万 的客户呢
推荐文章: