Laravel 分页不适用于 join where 条件
Laravel pagination not working with join where condtion
我正在为列表使用 laravel 分页。有两个表可以说 table1
& table2
。
table2
id
是主键并在 table1
t2_id
中用作外键
我想获得所有唯一的 t2_id
但需要检查 is_deleted 是否没有为表 2 中的那个 ID 设置。
我在我的模型中写了函数:
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 条件。
请提前帮忙谢谢!!
我得到了解决方案而不是distinct()
使用groupBy()
。现在它工作正常。 :)
引用自 laracast post : [L4.2] distinct() and pagination returns total number of results not distinct total
我正在为列表使用 laravel 分页。有两个表可以说 table1
& table2
。
table2
id
是主键并在 table1
t2_id
我想获得所有唯一的 t2_id
但需要检查 is_deleted 是否没有为表 2 中的那个 ID 设置。
我在我的模型中写了函数:
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 条件。
请提前帮忙谢谢!!
我得到了解决方案而不是distinct()
使用groupBy()
。现在它工作正常。 :)
引用自 laracast post : [L4.2] distinct() and pagination returns total number of results not distinct total