laravel 对获取的超大型json数据分页
2018年4月11日
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; class CubedataController extends Controller { public function show(Request $request) { $real = DB::table('blog')->where('uid', $uid)->select(); // 假设查出的数据 $perPage = 1; // 每页显示数量 if ($request->has('page')) { // 请求是第几页,如果没有传page数据,则默认为1 $current_page = $request->input('page'); $current_page = $current_page <= 0 ? 1 : $current_page; } else { $current_page = 1; } $item = array_slice($real, ($current_page - 1) * $perPage, $perPage); // 分页 $total = count($real); // 查询总数 $paginator = new LengthAwarePaginator($item, $total, $perPage, $current_page, [ 'path' => Paginator::resolveCurrentPath(), // 添加url 'pageName' => 'page', ]); return response()->json(['result' => $paginator]); } } |