Answer the question
In order to leave comments, you need to log in
How to get the number of elements in a query with having?
Tell me how to get the number of elements in the selection if there is a having in the query?
I try like this (a simplified version without a bunch of filters):
public function getListing($page = null, $count = null) {
$query = User::newQuery();
$table = User::getTable();
$query->select("{$table}.*", DB::raw('count(users.id) as images_count'));
$query->leftJoin('images', 'images.user_id', '=', "{$table}.id");
$query->havingRaw("images_count >= {$table}.image_limit");
$queryCount = clone $query;
$totalCount = $queryCount->distinct("{$table}.id")->count();
$query->groupBy("{$table}.id");
if (!is_null($page) && !is_null($count)) {
$query->skip(($page - 1) * $count);
};
if (!is_null($count)) {
$query->take($count);
};
$result = $query->get();
return [
$result,
$totalCount,
];
}
Column not found: 1054 Unknown column 'images_count' in 'having clause' (SQL: select count(*) as aggregate from `users` left join `images` on `images`.`user_id` = `users`.`id` having images_count >= users.image_limit
Answer the question
In order to leave comments, you need to log in
Hands through select () make a selection, without count ().
When using ->count() - the entire select is automatically killed, and accordingly your having throws an error.
The error shows that the query is not the same as you wrote: select count(*) as aggregate
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question