aBh*_*jit 12 pagination laravel laravel-5 laravel-pagination
Pagination::make() Laravel 5中的方法在Pagination类中不存在.
是否有解决方法在Laravel 5中进行手动分页工作?
Mar*_*łek 28
您需要添加使用:
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
Run Code Online (Sandbox Code Playgroud)
现在你可以使用:
$paginator = new Paginator($items, $count, $limit, $page, [
'path' => $this->request->url(),
'query' => $this->request->query(),
]);
Run Code Online (Sandbox Code Playgroud)
以与模型对象上的分页相同的格式获取数据;
使用示例Illuminate\Pagination\LengthAwarePaginator:
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
public function getItems(Request $request)
{
$items = []; // get array/collection data from somewhere
$paginator = $this->getPaginator($request, $items);
// now we can treat $paginator as an array/collection
return view('some-view')->with('items', $paginator);
}
private function getPaginator(Request $request, $items)
{
$total = count($items); // total count of the set, this is necessary so the paginator will know the total pages to display
$page = $request->page ?? 1; // get current page from the request, first page is null
$perPage = 3; // how many items you want to display per page?
$offset = ($page - 1) * $perPage; // get the offset, how many items need to be "skipped" on this page
$items = array_slice($items, $offset, $perPage); // the array that we actually pass to the paginator is sliced
return new LengthAwarePaginator($items, $total, $perPage, $page, [
'path' => $request->url(),
'query' => $request->query()
]);
}
Run Code Online (Sandbox Code Playgroud)
然后在 some-view.blade.php 文件中,例如:
@foreach($items as $item)
{{-- --}}
@endforeach
{{ $items->links() }}
Run Code Online (Sandbox Code Playgroud)
请参阅https://laravel.com/docs/5.7/pagination#manually-creating-a-paginator
| 归档时间: |
|
| 查看次数: |
14460 次 |
| 最近记录: |