Laravel 5.2中带有pagination()的distinct()不起作用 [英] distinct() with pagination() in laravel 5.2 not working

查看:175
本文介绍了Laravel 5.2中带有pagination()的distinct()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 laravel 5.2 中的distinct()distinct()一起使用,并且给出的结果正确,但分页保持不变(就像没有应用不同).

I'm trying to use distinct() with pagination() in laravel 5.2 with fluent and it's given result proper but pagination remain same(Like without apply distinct).

我已经检查并测试了以下带有防雷代码的答案
- laravel 5-对带有不同
-分页&独特
-使用单独的查询生成器分页方法计数错误

I have already reviewed and tested below answers with mine code
- laravel 5 - paginate total() of a query with distinct
- Paginate & Distinct
- Query Builder paginate method count number wrong when using distinct

我的代码如下:

DB::table('myTable1 AS T1')
->select('T1.*')
->join('myTable2 AS T2','T2.T1_id','=','T1.id')
->distinct()
->paginate(5);

示例
-我有3条记录(即 POST1,POST2,POST3 POST1 )的结果,所以我应用了distinct().
-现在我的结果是 POST1,POST2 POST3 ,但分页仍显示为4条记录(作为应用distinct()之前的结果).

EXAMPLE
- I have result with three records(i.e. POST1, POST2, POST3 and POST1) so I apply distinct().
- Now my result is POST1, POST2 and POST3 but pagination still display like 4 records(As result before applied distinct()).

任何建议将不胜感激!

推荐答案

Laravel以及在分页中使用distinct似乎存在一些持续的问题.

There seems to be some ongoing issues with Laravel and using distinct with pagination.

在这种情况下,当分页确定记录总数时,它将忽略您在select()子句中指定的字段.由于它会忽略您的列,因此独特功能也将被忽略.因此,计数查询变为select count(*) as aggregate from ...

In this case, when pagination is determining the total number of records, it is ignoring the fields you have specified in your select() clause. Since it ignores your columns, the distinct functionality is ignored as well. So, the count query becomes select count(*) as aggregate from ...

要解决此问题,您需要向分页功能介绍您的列.传递您要选择的列数组作为第二个参数,它将考虑它们的总数.因此,如果您这样做:

To resolve the issue, you need to tell the paginate function about your columns. Pass your array of columns to select as the second parameter, and it will take them into account for the total count. So, if you do:

/*DB::stuff*/->paginate(5, ['T1.*']);

这将运行计数查询:

select count(distinct T1.*) as aggregate from

因此,您的查询应如下所示:

So, your query should look like:

DB::table('myTable1 AS T1')
    ->select('T1.*')
    ->join('myTable2 AS T2','T2.T1_id','=','T1.id')
    ->distinct()
    ->paginate(5, ['T1.*']);

这篇关于Laravel 5.2中带有pagination()的distinct()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆