【发布时间】:2017-02-01 01:25:40
【问题描述】:
我已经设置了一个查询来通过搜索表单从数据库中搜索旅游,并对结果应用分页。以下是查询:
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 用以下数据填充(下拉)表单时。查询在视图中返回 4 个结果。
country:Croatia,category:Holiday, days: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,但没有遇到过这样的问题。
【问题讨论】:
-
调试堆栈跟踪会很有帮助。
-
你能打电话给localhost:8000/trip/…没有错误吗?
-
是的,它确实将我带到了包含所需数据的第 2 页。
-
试试我的分页答案。这里是动态链接stackoverflow.com/questions/41953914/…
标签: php mysql laravel pagination laravel-5.3