分页的方法;
// Illuminate\Pagination\Paginator
paginate() $users = DB::table('users')->paginate(15);
//Illuminate\Pagination\LengthAwarePaginator
simplePaginate() $users = DB::table('users')->simplePaginate(15);
Paginator
类不需要知道结果集中数据项的总数;不过,正因如此,该类也没有提供获取最后一页索引的方法。LengthAwarePaginator
接收参数和 Paginator
几乎一样,唯一不同在于它要求传入结果集的总数。
注:目前,使用 groupBy
的分页操作不能被 Laravel 有效执行,如果你需要在分页结果中使用 groupBy
,可以使用下面方法构建分页。
收到创建分页常用到offset,limit 或 skip , take
offset 设置从哪里开始,limit 设置想要查询多少条数据
Model::offset(0)->limit(10)->get();
skip,take (sikip 跳过几条,take 取多少条数据)
Model::skip(3)->take(3)->get();
$perPage = 5;
$page = $request->input("page",1)-1;
$total = DB::table("person")
->groupBy()
->count();
$items = DB::table("person")
->groupBy()
->skip($page*$perPage)
->take($perPage)
->get();
$person = new LengthAwarePaginator($items, $total,$perPage);
$person->withPath("pagTest");