当前位置 主页 > 网站技术 > 代码类 >

    Laravel5.5 手动分页和自定义分页样式的简单实现

    栏目:代码类 时间:2019-11-13 09:03

    基于Laravel5.5 在项目实施过程中,需要对从接口中获取的数据(或者通过搜索工具查询出来的数据)进行分页

    一、创建手动分页

    在laravel自带的分页中,一般是通过数据库查询访问paginate()方法来达到分页的效果 ,like this:

    class IndexControllerextends Controller

    {  
      publicfunctionindex()
      {
        $person = DB::table('person')->paginate(15);
     
        return view('index.pagTest',['person'=> $person]);
      }
    }

    查看框架的分页源代码

    #vender/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php

    /**
       * Paginate the given query.
       *
       * @param int $perPage
       * @param array $columns
       * @param string $pageName
       * @param int|null $page
       * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
       *
       * @throws \InvalidArgumentException
       */
      public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
      {
        $page = $page ?: Paginator::resolveCurrentPage($pageName);
     
        $perPage = $perPage ?: $this->model->getPerPage();
     
        $results = ($total = $this->toBase()->getCountForPagination())
                      ? $this->forPage($page, $perPage)->get($columns)
                      : $this->model->newCollection();
     
        return $this->paginator($results, $total, $perPage, $page, [
          'path' => Paginator::resolveCurrentPath(),
          'pageName' => $pageName,
        ]);
      }
    
    

    发现,分页用了 \Illuminate\Contracts\Pagination\LengthAwarePaginator 构造方法,查看这个构造方法

    <?php
     
    namespace Illuminate\Pagination;
     
    use Countable;
    use ArrayAccess;
    use JsonSerializable;
    use IteratorAggregate;
    use Illuminate\Support\Collection;
    use Illuminate\Support\HtmlString;
    use Illuminate\Contracts\Support\Jsonable;
    use Illuminate\Contracts\Support\Arrayable;
    use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract;
     
    class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, LengthAwarePaginatorContract
    {
      /**
       * The total number of items before slicing.
       *
       * @var int
       */
      protected $total;
     
      /**
       * The last available page.
       *
       * @var int
       */
      protected $lastPage;
     
      /**
       * 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);
      }