//routes\web.php
Route::get('/v5', [TestVIewController::class, 'v5']);
Route::get('/v55', [TestVIewController::class, 'v55']);
//app\Http\Controllers\TestViewController.php
public function v5()
{
$datas = DB::table('users')->paginate(2);
return view('test.v5', compact('datas'));
}
public function v55(Request $request)
{
$sql = "select * from users";
$data = DB::select($sql);
$datas = $this->arrayPaginator($data, $request);
return view('test.v5', compact('datas'));
}
public function arrayPaginator($array, $request)
{
$page = $request->get('page', 1);
$perPage = 2;
$offset = ($page * $perPage) - $perPage;
return new \Illuminate\Pagination\LengthAwarePaginator(
array_slice(
$array,
$offset,
$perPage,
true
),
count($array),
$perPage,
$page,
['path' => $request->url(), 'query' => $request->query()]
);
}
//resources\views\test\v5.blade.php
<link rel="stylesheet" type="text/css" href="{{asset('css')}}/app.css" />
<script type="text/javascript" src="{{asset('js')}}/jquery.min.js"></script>
<script type="text/javascript" src="{{asset('js')}}/bootstrap.min.js"></script>
@extends('mytemplate.parent')
@section('part1')
<table class="table">
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
</tr>
@foreach($datas as $d)
<tr>
<td>{{$d->id}}</td>
<td>{{$d->name}}</td>
<td>{{$d->age}}</td>
</tr>
@endforeach
</table>
{{ $datas->withQueryString()->links('pagination::bootstrap-4') }}
{{ $datas->links() }}
@endsection
@section('part2')
当前页数据条数:{{$datas->count()}}<br/>
当前页码:{{$datas->currentPage()}}<br/>
当前页第一条数据的序号:{{$datas->firstItem()}}(这个是指在数据总条数里的序号)<br/>
当前页最后一条数据的序号:{{$datas->lastItem()}}<br/>
是否有下一页:{{$datas->hasMorePages()}}<br/>
最后一页序号:{{$datas->lastPage()}}<br/>
上一页的url:{{$datas->previousPageUrl()}}<br/>
下一页的url:{{$datas->nextPageUrl()}}<br/>
指定页的url:{{$datas->url(5)}}<br/>
每一页显示数据条数:{{$datas->perPage()}}<br/>
数据总条数:{{$datas->total()}}<br/>
<?php var_dump($datas);?>
@endsection
遇到一些问题,以下是网上找到的解决方案,原因均不明
- 未使用simplePaginate时也不显示中间的页数,把 {{ $datas->links() }} 改成 {{ $datas->withQueryString()->links(‘pagination::bootstrap-4’) }}即可
- v55是使用sql文检索的分页显示