使用 paginate 分页的同时 distinct 去重,会导致分页数出错
$result = $db->table('k_flow')
->join('k_flow_operate', 'k_flow_operate.flow_id', '=', 'k_flow.flow_id')
->where('k_flow.is_finish', 2)
->where('k_flow_operate.operator', $userName)
->whereIn('k_flow_operate.status', [2, 3, 4, 5, 6, 7, 8, 9])
->orderBy('k_flow.add_time', 'desc')
->select('k_flow.*')
->distinct()
->paginate(15);
执行以上语句时,会返回如下结果
LengthAwarePaginator {#348 ▼
#total: 16
#lastPage: 2
#items: Collection {#335 ▼
#items: array:8 [▼
0 => {#338 ▶}
1 => {#331 ▶}
2 => {#339 ▶}
3 => {#332 ▶}
4 => {#341 ▶}
5 => {#324 ▶}
6 => {#330 ▶}
7 => {#340 ▶}
]
}
#perPage: 15
#currentPage: 1
#path: "http://config.admin.kugou.net/config/setting/flow/2"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: array:2 [▶]
}
可以看到,实际上去重后的数据是 8 条,但是分页里的数据还是按照没去重之前的输出,这样导致了分页出错。
请问有哪位大佬遇到过类似的问题么?有没有解决的方案提供?
ps:网上查阅时,有人说 paginate 有第二个参数,然鹅,文档里写的很明确:
传递给 paginate
方法的唯一参数是你希望 "每页" 展示的项目数量
推荐文章: