带着问题去思考
paginate()
方法返回的是什么
links()
方法代码如何执行
以最简单的分页代码为例引出具体内容
具体方法使用看官方文档
$users = DB::table('admin_operation_log')->paginate(3);
根据上面代码可知调用Facades门面的是Illuminate\Database\Query
下的Builder
类(俗称查询构造器)下paginate
方法
/**
* Paginate the given query into a simple paginator.
*
* @param int $perPage
* @param array $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);
$total = $this->getCountForPagination($columns);
$results = $total ? $this->forPage($page, $perPage)->get($columns) : collect();
return $this->paginator($results, $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
}
$perPage 每页显示的数量
$columns 查询的字段名称
$pageName 页名
$page 当前页
$this->paginator($results, $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
])
这里调用trait
BuildsQueries
类下的paginator
方法
/**
* Create a new length-aware paginator instance.
*
* @param \Illuminate\Support\Collection $items
* @param int $total
* @param int $perPage
* @param int $currentPage
* @param array $options
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
protected function paginator($items, $total, $perPage, $currentPage, $options)
{
return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
'items', 'total', 'perPage', 'currentPage', 'options'
));
}
到这里就可知DB::table('admin_operation_log')->paginate(3)
返回的是LengthAwarePaginator
类的实例
看一下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
*/
public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
{
foreach ($options as $key => $value) {
$this->{$key} = $value;
}
$this->total = $total;
$this->perPage = $perPage;
$this->lastPage = max((int) ceil($total / $perPage), 1);
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
$this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
$this->items = $items instanceof Collection ? $items : Collection::make($items);
}
$items 数据的集合
$total 总条数
$perPage 每页显示的数量
$currentPage 当前页
$options (path, query, fragment, pageName)
再来看一下视图$users->links()
方法,这里的links
方法是 LengthAwarePaginator
类下的
/**
* Render the paginator using the given view.
*
* @param string|null $view
* @param array $data
* @return \Illuminate\Support\HtmlString
*/
public function links($view = null, $data = [])
{
return $this->render($view, $data);
}
这里的render
方法是 LengthAwarePaginator
类下的
/**
* Render the paginator using the given view.
*
* @param string|null $view
* @param array $data
* @return \Illuminate\Support\HtmlString
*/
public function render($view = null, $data = [])
{
return new HtmlString(static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
'paginator' => $this,
'elements' => $this->elements(),
]))->render());
}
这里又调用了HtmlString
类返回是HtmlString
类的实例,HtmlString
类作用是把对象当做字符串调用时会触发里面的__toString
函数进行输出
最终数据会流向 Symfony\Component\HttpFoundation
下的Response
类sendContent
方法
/**
* Sends content for the current web response.
*
* @return $this
*/
public function sendContent()
{
echo $this->content;
return $this;
}
这里的$this->content
就是Htmlstring
的实例,echo
的时候就会调用__toString
方法进行数据输出
- 终
如有不对的地方请及时指正