php Laravel 架構之分頁

來源:互聯網
上載者:User

標籤:php   laravel   分頁   

今天學習了Laravel的分頁功能,感覺它這個非常的好用.
下面拿出來和大家分享一下.
首先第一步,我們需要擷取到查詢的結果.
方法大家應該各有所異,無非包括各種條件,排序.但是最後我們必須通過
paginate(PAGESIZE)來擷取選定的結果.
例如:我使用Eloquent 來擷取資料.
$ret = User::where(‘age‘,‘gt‘,25)->orderBy(‘sex‘,‘asc‘)->paginate();
好了,我們需要將它們帶入視圖當中.
return View::make(‘user.index‘)->with(‘results‘,$ret);

//這裡需要解釋一下,這裡的user.index表示的是 在views檔案夾下的user檔案夾下的index.blade.php
視圖模板檔案.
學過Laravel的都明白呵.
在index.blade.php中.
我們通過模板迴圈輸出.

例如

<table class="table table-bordered table-striped">

<tr>

    <th>id</th>

    <th>name</th>

    <th>sex</th>

    <th>Action</th>

</tr>

@foreach($results as $v)

<tr>

    <td>{{$v->id}}</td>

    <td>{{$v->name}}</td>

    <td>{{$v->sex}}</td>

    <td>action</td>

</tr>

@endforeach

<tfoot>

<tr>

    <td colspan=4>{{$results->links()}}</td>

</tr>

</tfoot>

</table>

好了,這裡最關鍵的就是我們的tfoot裡面的內容了...

它能夠自動產生我們的分頁.

這裡我們要注意一點.

最終產生的串連如 http://localhost/party/public/notice?page=2

這樣我們就能夠正常的進行翻頁操作了.


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.