0
votes

I have made this query:

        $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 . '%')
    ;

Even I have followed this solution but I got this error :

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

1
which table has the total column here? - sta
@sta , total is an alias of count(user_history_accesses.id) not column - hous

1 Answers

3
votes

you can not use result colum in the where, you should recalculate it in the where to get the desired result, but try using 'having':

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