带着问题去思考
复杂的sql需要用原生的写法如何分页
以最简单的代码为例引出具体内容
具体方法使用看官方文档
$users = DB::select('select * from users where active = ?', [1]);
根据上面代码可知调用的是Illuminate\Database
下的Connection
类下select
方法,该方法下没有分页方法,
根据上文分析分页原理可以手写分页,调用对应的方法即可
$perPage = 5;
$page = $request->get("page",1)-1;
$total = DB::table("admin_operation_log")->count();
$items = DB::table("admin_operation_log")
->skip($page*$perPage)
->take($perPage)
->get();
$currentPage = null;
$users =new LengthAwarePaginator($items, $total, $perPage, $currentPage, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
return $res = $users->links();
- 终
如有不对的地方请及时指正