分页:条件分页第一页正常,第二页异常,求大神指点,已附简洁代码。
1. 运行环境
1)Laravel 5.3
2)php7.4
3)CentOS 7.2
2. 分页:条件分页第一页正常,第二页异常,求大神指点
public function index(Request $request)
{
$start = $request->start;
$end = $request->end;
$type = $request->type;
if ($start != null && $end != null){
$logs = Log::where('type',$type)
->whereDate('created_at','>=',$start)
->whereDate('created_at','<=',$end)
->orderBy('created_at','desc')
->paginate(16);
return view('log.index',compact('logs'));
}
$logs = Log::paginate(16);
return view('log.index',compact('logs'));
}
视图
{{-- 查询表单 --}}
<form method="GET" action="{{ route('log.index') }}" >
<span>开始日期:</span>
<input type="date" name="start" value="" required />
<br>
<span>结束日期:</span>
<input type="date" name="end" value="" required />
<span>日志类型:</span>
<input type="text" name="type" value="" required>
<button type="submit" >查询</button>
</form>
{{-- 日志 --}}
@foreach($logs as $log)
.
.
.
@endforeach
{{-- 分页 --}}
<div class="page">
{{$logs->links()}} <span>共有数据:{{$logs->total()}} 条</span>
</div>
3. 结果
无查询时一切正常;
有查询时,第一页正常,第二页异常。
翻页后异常,是因为翻页后搜索条件的丢失,你需要在 自定义分页的 URL