Eloquent tally different from incorrect totals

I have a problem with how eloquent the wording of the request is, to which I do not have access. When you do something like

$model->where('something')
->distinct()
->paginate();

eloquent runs the query to get the total, and the query looks something like this:

select count(*) as aggregate from .....

The problem is that if you use a different query, you want something like

select count(distinct id) as aggregate from .....

to get the right amount. The eloquent does not do this, although, thus, returning the wrong results. The only way to get the report in the invoice is to pass the argument through the query builder, for example: → count ('id'), in which case it will add it. The problem is that this request is auto-generated, and I do not control it.

Is there a way to trick it into adding a separate count to the request?

P.S. , IF, count(), . \Database\Query\\BaseGrammar @compileAggregate

if ($query->distinct && $column !== '*')
{
   $column = 'distinct '.$column;
}
return 'select '.$aggregate['function'].'('.$column.') as     aggregate';

PS1 , SQL , , , IN ( ) , .

+4
3

, , , :

https://github.com/laravel/framework/issues/3191
https://github.com/laravel/framework/pull/4088

, Laravel, , ( , )

$stuff = $model->select(DB::raw('distinct id as did'))
  ->where('whatever','=','whateverelse')
  ->paginate();

: http://laravel.com/docs/queries#raw-expressions

0
$model->where('something')->distinct()->count('id')->paginate();
0

I ran into the same problem and found two solutions:

Bad:

$results = $model->groupBy('foo.id')->paginate();

It works, but it will cost too much memory (and time) if you have a large number of lines (this was my case).

It's better:

$ids = $model->distinct()->pluck('foo.id');
$results =  $query = $model->whereIn('foo.id', $ids)->paginate();

I tried this with 100k results and had no problems.

0
source

Source: https://habr.com/ru/post/1540799/


All Articles