【问题标题】:Laravel pagination not working with join where condtionLaravel 分页不适用于 join where 条件
【发布时间】:2017-09-14 19:42:10
【问题描述】:

我正在使用 laravel 分页列表。有两张表可以说table1table2

table2 id 是主键,在table1 中用作外键 t2_id

我想获取所有唯一的t2_id,但需要检查表 2 中的该 ID 是否设置了 is_deleted。

我已经在我的模型中写了函数:

public function getDistinctIds() {

    return DB::connection('pgsql')
                    ->table('table1 AS t1')
                    ->distinct()
                    ->select('t1.t2_id')
                    ->join('table2 AS t2', 't2.id', '=', 't1.t2_id')
                    ->where('t2.is_deleted', '!=', 1)
                    ->orWhereNull('t2.is_deleted')
                    ->paginate(3);
}

这个功能会给我准确的结果。但是分页有问题。 table1 有 12 条记录 getDistinctIds() 只给出 4 条记录,所以应该有两页,但它显示了 4 页。我认为分页没有考虑 where 条件。

请帮忙提前谢谢!!

【问题讨论】:

  • getDistinctIDs 表是什么?转储查询并在数据库上运行它,您可能需要左连接或运算符优先级会弄乱您的 WHERE 条件
  • @Mihai getDistinctIDs 是模型中的函数返回。它给出了准确的结果,它返回 4 行,所以应该有 2 页,但它显示了 4 页链接

标签: php pagination laravel-5.2


【解决方案1】:

我得到了解决方案,而不是 distinct()used groupBy()。现在它工作正常。 :)

引用自 laracast 帖子:[L4.2] distinct() and pagination returns total number of results not distinct total

【讨论】:

    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多