我已经设置了一个查询以通过搜索表单从数据库中搜索游览并对结果应用分页。以下是查询:
public function search(Request $request)
{
$days = $request->days;
$days_explode = explode('|', $days);
$query= Tour::whereHas('country', function($r) use($request) {
$r->where('countries.name','=', $request->country);
})
->whereHas('category', function($s) use($request) {
$s->where('categories.name','=', $request->category);
})
->whereHas('country', function($r) use($request) {
$r->where('countries.name','=', $request->country);
})
->whereBetween('days', [$days_explode[0], $days_explode[1]])
->paginate(1);
return view('public.tour.list')->withResults($query);
}
在不使用 paginate
的情况下使用以下数据填充(下拉)表单。查询在 View 中返回 4 个结果。
国家:克罗地亚,类别:假日,天数:10|15
当我使用 paginate()
在多个页面中获取结果时,起初它运行时没有错误但是当我单击其他页面时,如 2,3。报错
undefined offset :1
搜索页面的 url 如下所示:
http://localhost:8000/trip/search?country=croatia&category=holiday&days=10%7C15
第 2 页的 URL 如下所示:
http://localhost:8000/trip/search?page=2
给出错误
`Undefined offset: 1`
我之前在 laravel 中使用过 paginate 但没有遇到过这样的问题。
最佳答案
这里的问题是,当您转到下一页时,您不会携带查询字符串数据,因此您需要将此数据传递给您的 View ,您可以使用以下内容
{{ $data->appends(Request::only('country','categorry','days'))->links() }}
这应该可以解决
关于php - undefined offset : 1 during pagination Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41963493/