我正在尝试使用 laravel eloquent 来处理查询,但它无法识别关系
$query = $site
->consumers()
->with([
'transactions' => function($_query) use ($thresh) {
$_query
->where('status', 'processed')
->where('approved', 1)
->selectRaw("SUM({$thresh} - value + bonus_value) AS total_points");
}
])
->where('total_points', '<=', 5000);
dd($query->get()->toArray());
我有这个代码。当我添加关系时selectRaw
,transactions
它什么都不返回,但是当我删除它时,它返回与消费者关联的所有交易。此外,total_points
inwhere
子句提出了total_points
未找到的问题。
我基本上是在尝试获得总分小于或等于 5000 的消费者。