Last active
November 14, 2019 06:55
-
-
Save imzhi/f149440ed097aed00a47a8c82a204aa6 to your computer and use it in GitHub Desktop.
手动构造 laravel paginate 数据
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $list_operation = AppListXianwanLog::where(function ($query) use ($args) { | |
| $name = $args['name'] ?? ''; | |
| if ($name) { | |
| $query->whereHas('appXianwan', function ($hasQuery) use ($name) { | |
| $hasQuery->where('adname', 'like', "%{$name}%"); | |
| }); | |
| } | |
| }) | |
| ->groupBy('adid') | |
| ->orderBy('adid') | |
| ->select($cols) | |
| ->paginate(config('admin.pagination.num')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $list_operation = AppListXianwanLog::where(function ($query) use ($args) { | |
| $name = $args['name'] ?? ''; | |
| if ($name) { | |
| $query->whereHas('appXianwan', function ($hasQuery) use ($name) { | |
| $hasQuery->where('adname', 'like', "%{$name}%"); | |
| }); | |
| } | |
| }) | |
| ->groupBy('adid') | |
| ->orderBy('adid') | |
| ->get($cols); | |
| $list_operation_r = $list_operation->slice(config('admin.pagination.num') * (($args['page'] ?? 1) - 1), config('admin.pagination.num')); | |
| $list_operation = new LengthAwarePaginator($list_operation_r, $list_operation->count(), config('admin.pagination.num'), $args['page'] ?? null, [ | |
| 'path' => request()->url(), | |
| 'query' => request()->query(), | |
| ]); |
Author
Author
这里的第一条查询语句不能用 paginate 方法,只能用 get 方法,因为进行了 groupBy 分组查询。
manual_laravel_pagination.php 中的 groupBy 查询也可以使用 pagination 方法自动进行分页查询,不需要手动构造分页。新增 auto_laravel_pagination.php 实现需要。manual_laravel_pagination.php 仍然保留作为手动构造分页的参考。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
先查出所有的数据;将所有数据根据页码和每页条数进行剪裁;通过 LengthAwarePaginator 传入指定参数构造出符合的 paginator。