在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()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)