0

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

2
  • which table has the total column here? Commented Nov 6, 2020 at 15:50
  • @sta , total is an alias of count(user_history_accesses.id) not column Commented Nov 6, 2020 at 15:52

1 Answer 1

3

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 . '%'); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.