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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41283083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:58:40  来源:igfitidea点击:

distinct() with pagination() in laravel 5.2 not working

phplaravelpaginationlaravel-5.2fluent

提问by AddWeb Solution Pvt Ltd

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

我正在尝试在laravel 5.2 中使用distinct()with pagination(),并且它给出了正确的结果,但分页保持不变(就像没有应用不同)。

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

我已经使用我的代码查看并测试了以下答案
- laravel 5 - paginate total() of a query with distinct
- Paginate & Distinct
- Query Builder paginate method count number wrong when using distinct

My code is something like:

我的代码是这样的:

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

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

示例
- 我有三个记录(即POST1、POST2、POST3POST1)的结果,所以我申请了distinct()
- 现在我的结果是POST1、POST2POST3但分页仍然显示为 4 条记录(作为应用前的结果distinct())。

Any suggestion would be appreciated!

任何建议将不胜感激!

回答by patricus

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

Laravel 和使用 distinct 与分页似乎存在一些持续的问题。

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 ...

在这种情况下,当分页确定记录总数时,它会忽略您在select()子句中指定的字段。由于它忽略了您的列,因此也忽略了不同的功能。因此,计数查询变为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:

要解决此问题,您需要告诉 paginate 函数有关您的列。传递您的列数组以选择作为第二个参数,它会将它们计入总计数。所以,如果你这样做:

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

This will run the count query of:

这将运行以下计数查询:

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.*']);

回答by Gevorg Melkumyan

If it is possible, you can change distinct()with groupBy().

如果可能的话,你可以改变distinct()使用groupBy()

回答by AddWeb Solution Pvt Ltd

I found the problem at my code, that is I need to pass $columnas second parameter into paginate($perPage, $columns, $pageName, $page). I got the solution from laravel git issue.

我在我的代码中发现了问题,即我需要将$column第二个参数传递给paginate($perPage, $columns, $pageName, $page). 我从laravel git issue得到了解决方案。

So my working code:

所以我的工作代码:

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

Need suggestion
What if, I hack the code for Builder.phpto bypass passing the second parameter $column. I mean this is the good thing OR any batter option?

需要建议
如果,我破解了Builder.php的代码以绕过传递第二个参数$column。我的意思是这是好事还是任何击球手的选择?

/**
 * Paginate the given query into a simple paginator.
 *
 * @param  int  $perPage
 * @param  array  $columns
 * @param  string  $pageName
 * @param  int|null  $page
 * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
 */
public function paginate($perPage = 15, $columns = null, $pageName = 'page', $page = null)
{

    //To solved paginator issue with distinct...
    if(is_null($columns) && strpos($this->toSql(), 'distinct') !== FALSE){
        $columns = $this->columns; 
        $columns = array_filter($columns, function($value) {
            return (is_string($value) && !empty($value));
        });
    }
    else {
        //If null $column, set with default one
        if(is_null($columns)){
            $columns = ['*'];
        }
    }

    $page = $page ?: Paginator::resolveCurrentPage($pageName);

    $total = $this->getCountForPagination($columns);

    $results = $total ? $this->forPage($page, $perPage)->get($columns) : [];

    return new LengthAwarePaginator($results, $total, $perPage, $page, [
        'path' => Paginator::resolveCurrentPath(),
        'pageName' => $pageName,
    ]);
}

回答by prateekkathal

The distinct is working.... When you make a join, it checks for distinct entries in all selected columns and here.. the column names you get after the join are also included in the join...

独特的工作......当您进行连接时,它会检查所有选定列和此处的不同条目......您在连接后获得的列名称也包含在连接中...

To run this please add select()to your builder instance and it should work :)

要运行它,请添加select()到您的构建器实例中,它应该可以工作:)

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

Let me know if this doesn't work for you :)

如果这对您不起作用,请告诉我:)