laravel的分页问题实例

零下一度
Lepaskan: 2023-03-13 07:44:01
asal
1665 orang telah melayarinya

<span class="token scope"><span class="token punctuation"><span class="token function">分页是在网页上是很常用的,基本上每个网页都有。首先我们看下laravel得分页方法源码:</span></span></span>

<code class="hljs php has-numbering"><span class="hljs-comment">#vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:480<span class="hljs-keyword">public <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">paginate<span class="hljs-params">(<span class="hljs-variable">$perPage = null, <span class="hljs-variable">$columns = [<span class="hljs-string">&#39;*&#39;], <span class="hljs-variable">$pageName = <span class="hljs-string">&#39;page&#39;, <span class="hljs-variable">$page = null)
{<span class="hljs-variable">$query = <span class="hljs-variable">$this->toBase();<span class="hljs-variable">$total = <span class="hljs-variable">$query->getCountForPagination();<span class="hljs-variable">$this->forPage(<span class="hljs-variable">$page = <span class="hljs-variable">$page ?: Paginator::resolveCurrentPage(<span class="hljs-variable">$pageName),<span class="hljs-variable">$perPage = <span class="hljs-variable">$perPage ?: <span class="hljs-variable">$this->model->getPerPage()
        );<span class="hljs-keyword">return <span class="hljs-keyword">new LengthAwarePaginator(<span class="hljs-variable">$this->get(<span class="hljs-variable">$columns), <span class="hljs-variable">$total, <span class="hljs-variable">$perPage, <span class="hljs-variable">$page, [<span class="hljs-string">&#39;path&#39; => Paginator::resolveCurrentPath(),<span class="hljs-string">&#39;pageName&#39; => <span class="hljs-variable">$pageName,
        ]);
}<br/>我们发现这个关键就是用了lengthAwarePaginator.<br/>  LengthAwarePaginator的构造方法,如下:<br/></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Salin selepas log masuk
假定分页得数组为:
Salin selepas log masuk
[
  {
    "id": 9,
    "sys_id": 1,
    "org_id": 2,
    "user_id": 8,
    "papaer_id": 26,
  },
  {
    "id": 5,
    "sys_id": 1,
    "org_id": 2,
    "user_id": 8,
    "papaer_id": 26,
  },
  {
    "id": 1,
    "sys_id": 1,
    "org_id": 2,
    "user_id": 8,
    "papaer_id": 26,
  }
]
Salin selepas log masuk
这里一共3条数据,我们设定每页显示1个,一共3页。
分页调用
   use Illuminate\Pagination\LengthAwarePaginator;
  use Illuminate\Pagination\Paginator;

  public function show(Request $request){
        $perPage = 1;            //每页显示数量
        if ($request->has(&#39;page&#39;)) {
                $current_page = $request->input(&#39;page&#39;);
                $current_page = $current_page <= 0 ? 1 :$current_page;
        } else {
                $current_page = 1;
        }
        $item = array_slice($real, ($current_page-1)*$perPage, $perPage); //注释1
        $total = count($real);

        $paginator =new LengthAwarePaginator($item, $total, $perPage, $current_page, [
                &#39;path&#39; => Paginator::resolveCurrentPath(),  //注释2
                &#39;pageName&#39; => &#39;page&#39;,
        ]);
          return response()->json([&#39;result&#39;=>$paginator])
   }
Salin selepas log masuk

  上面的代码中的重点是$item,如果不做注释1处理,得出的是所有7条数据。

  注释2处就是设定个要分页的url地址。也可以手动通过 $paginator ->setPath(‘路径’) 设置。

  页面中的分页连接也是同样的调用方式 {{ $paginator->render() }}

注:返回得数据格式大概如下所示:

"result": {
    "current_page": 3,
    "data": [
    { 
      "id": 5, 
      "sys_id": 1,
      "org_id": 2,
       "user_id": 8, 
          "papaer_id": 26
     }
    ],
    "from": null,
    "last_page": 2,
    "next_page_url": null,
    "path": "http://www.text.tld/examination/show",
    "per_page": 2,
    "prev_page_url": "http://www.text.tld/examination/show?page=2",
    "to": null,
    "total": 3
  }
Salin selepas log masuk

Atas ialah kandungan terperinci laravel的分页问题实例. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan