沃梦达 / 编程问答 / php问题 / 正文

在laravel 5.2中使用分页()的distinct()不起作用

distinct() with pagination() in laravel 5.2 not working(在laravel 5.2中使用分页()的distinct()不起作用)

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

问题描述

我正在尝试在 laravel 5.2 中使用 distinct()pagination() 流利,结果正确,但分页仍然存在相同(就像没有应用不同).

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 - 使用不同的分页对 total() 查询/a>
- 分页 &与众不同
- 使用 distinct 时查询生成器分页方法计数错误

我的代码类似于:

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

示例
- 我得到了三个记录(即 POST1、POST2、POST3POST1)的结果,所以我应用了 distinct().
- 现在我的结果是 POST1、POST2POST3 但分页仍然显示为 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中使用分页()的distinct()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在laravel 5.2中使用分页()的distinct()不起作用

基础教程推荐