使用 where 时列总数未知

Unknown column total when using where

我查询过:

        $query = UserHistoryAccess::with('user')
        ->select('user_history_accesses.*', DB::raw('count(user_history_accesses.id) as total'))
        ->join('users', 'user_history_accesses.user_id', 'users.id')
        ->groupBy('user_history_accesses.user_id')
        ->where('total', 'like', '%' . $term . '%')
    ;

即使我已经按照这个 但是我得到了这个错误:

Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'total' in 'where clause' (SQL: select count(*) as aggregate from user_history_accesses inner join users on user_history_accesses.user_id = users.id where total like %388%) in file C:\xampp\htdocs\HabilitisBack\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 671

您不能在 where 中使用结果列,您应该在 where 中计算它以获得所需的结果,但请尝试使用 'having':

 ->having('total', 'like', '%' . $term . '%');