Laravel手动创建分页器
2017-05-05 20:20
537 查看
Laravel手动创建分页器
laravel自带的分页功能十分强大,只不过,在使用 groupBy 语句的分页操作时,无法由 Laravel 有效执行。如果你需要在一个分页结果集中使用groupBy,建议你查询数据库并手动创建分页器。(基于Laravel5.4,老版本可能需要修改个别地方)
默认分页功能示例代码如下:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class IndexControllerextends Controller
{
publicfunctionindex()
{
$person = DB::table('person')->paginate(15);
return view('index.pagTest',['person'=>
$person]);
}
}
手动创建分页器,代码如下;
$item //手动查询到的数据
$total //所有符合条件的数据总数量
$perPage //每个页面,数据显示的条数
LengthAwarePaginator构造函数,代码如下
/**
* Create a new paginator instance.
*
* @param mixed $items
* @param int $total
* @param int $perPage
* @param int|null $currentPage
* @param array $options (path, query, fragment, pageName)
* @return void
*/
publicfunction
__construct($items,$total,$perPage,$currentPage=null,array$options=
[])
{
foreach ($optionsas$key=>$value)
{
$this->{$key} =$value;
}
$this->total=$total;
$this->perPage=$perPage;
$this->lastPage= (int)ceil($total/$perPage);
$this->path=$this->path!='/'?rtrim($this->path,'/')
:$this->path;
$this->currentPage=$this->setCurrentPage($currentPage,$this->pageName);
$this->items=$itemsinstanceofCollection
?$items: Collection::make($items);
}
附上结果图(因为使用了路由,请忽略url的不同:)
laravel自带的分页功能十分强大,只不过,在使用 groupBy 语句的分页操作时,无法由 Laravel 有效执行。如果你需要在一个分页结果集中使用groupBy,建议你查询数据库并手动创建分页器。(基于Laravel5.4,老版本可能需要修改个别地方)
默认分页功能示例代码如下:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class IndexControllerextends Controller
{
publicfunctionindex()
{
$person = DB::table('person')->paginate(15);
return view('index.pagTest',['person'=>
$person]);
}
}
手动创建分页器,代码如下;
<?php namespace App\Http\Controllers\Index; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\DB; use Illuminate\Pagination\LengthAwarePaginator; class IndexController extends Controller { //测试查询数据库并手动创建分页器 public function paginationTest(Request $request){ $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"); return view("index.paginationTest",['person' => $person]); } }
讲解:理解LengthAwarePaginator构造函数的前三个参数,就能明白如何手动创建分页器
$person->withPath("pagTest")是设置baseUrl的方法
$item //手动查询到的数据
$total //所有符合条件的数据总数量
$perPage //每个页面,数据显示的条数
LengthAwarePaginator构造函数,代码如下
/**
* Create a new paginator instance.
*
* @param mixed $items
* @param int $total
* @param int $perPage
* @param int|null $currentPage
* @param array $options (path, query, fragment, pageName)
* @return void
*/
publicfunction
__construct($items,$total,$perPage,$currentPage=null,array$options=
[])
{
foreach ($optionsas$key=>$value)
{
$this->{$key} =$value;
}
$this->total=$total;
$this->perPage=$perPage;
$this->lastPage= (int)ceil($total/$perPage);
$this->path=$this->path!='/'?rtrim($this->path,'/')
:$this->path;
$this->currentPage=$this->setCurrentPage($currentPage,$this->pageName);
$this->items=$itemsinstanceofCollection
?$items: Collection::make($items);
}
附上结果图(因为使用了路由,请忽略url的不同:)
相关文章推荐
- Laravel 手动创建分页器 LengthAwarePaginator
- Laravel手动创建分页器
- laravel手动创建分页
- 深入理解Laravel框架--手动创建laravel框架(2)
- Laravel5.4手动创建分页
- 深入理解Laravel框架--手动创建laravel框架(1)
- laravel使用artisan创建迁移后手动删除迁移文件报错解决方法
- laravel手动创建数组分页的实现代码
- oracle手动创建数据库
- 手动创建第一个OC程序
- 手动地通过命令行的方式创建一个数据库
- Linux下手动创建用户账号
- 手动创建数据库的一点问题ORA-02165
- laravel创建定时任务
- 【3】创建一个简单的Laravel例子
- Linux系统下手动创建用户的详细步骤
- 用eclipse创建动态web项目手动生成web.xml方法
- Oracle 11g 手动创建数据库 windows下
- Laravel学习1-创建项目
- javaWeb 手动建目录树创建web应用并部署