標籤: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
這樣我們就能夠正常的進行翻頁操作了.