我有这样的json
{#232 ▼
+"current_page": 1
+"data": array:2 [▼
0 => {#230 ▼
+"id": 1
+"test_col": "Test one"
}
1 => {#237 ▼
+"id": 3
+"test_col": "Test three"
}
]
+"first_page_url": "http://api/api/test?page=1"
+"from": 1
+"last_page": 8
+"last_page_url": "http://api/api/test?page=8"
+"next_page_url": "http://api/api/test?page=2"
+"path": "http://api/api/test"
+"per_page": 2
+"prev_page_url": null
+"to": 2
+"total": 15
}
为了得到分页,我正在做这些代码
$response = $response->getBody()->getContents();
$outputs = (json_decode($response));
$outputs = (json_decode($response));
$pagination = new LengthAwarePaginator($outputs->data, $outputs->total, $outputs->per_page, $outputs->current_page);
return view('test.index')->with('outputs',$outputs)->with('pagination',$pagination);
但是当我单击第2页时,我将重定向到我的索引页,当我这样做时
dd($pagination->links());
我看到了这个结果
<ul class="pagination" role="navigation">
<li class="page-item disabled" aria-disabled="true" aria-label="« Previous">
<span class="page-link" aria-hidden="true">‹</span>
</li>
<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>
<li class="page-item"><a class="page-link" href="/?page=2">2</a></li>
<li class="page-item"><a class="page-link" href="/?page=3">3</a></li>
<li class="page-item"><a class="page-link" href="/?page=4">4</a></li>
<li class="page-item">
<a class="page-link" href="/?page=2" rel="next" aria-label="Next »">›</a>
</li>
</ul>
答案 0 :(得分:0)
Laravel带有默认的pagination系统。
通过控制器获取数据,例如
$perPage = 10;
$data = MyModel::paginate($perPage);
return view('test.index', compact('data'));
然后在刀片视图中显示它:
<div class="container">
@foreach ($data $d)
{{-- Display the data here --}}
@endforeach
</div>
分页链接将自动添加
{{ $data-links() }}