If I am trying to display the total number of distinct customers, it would as simple as follows:
return $this->count(
$request,
Order::select('customer_id')->distinct(),
'customer_id'
);
But what if I am trying to display the total number of repeating customers. Repeating customers are those who appear multiple times in orders
table. What I am thinking of is as follows:
$query = Order::select(DB::raw('count(*) as count, customer_id'))
->groupBy('customer_id')
->having('count', '>', 1);
return $this->sum(
$request,
$query,
'count'
);
But I keep getting the following error:
message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in 'field list' (SQL: select sum(`count`) as aggregate from `orders` where `created_at` between 2020-10-31 23:56:53 and 2020-11-30 23:56:53 group by `customer_id` having `count` > 1)"
How can I fix such issue?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…