I have setup a query to search tours from the database via search form and applied pagination to the result. Below is the query:
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);
}
When form is filled (dropdown) with below data without using paginate
. Query returns 4 results in view.
country:Croatia,category:Holiday, days:10|15
And when I use paginate()
to get results in multiple page, at first it runs without error But when I click other page like 2,3. It gives error
Undefined offset: 1
The url of search page looks like this:
http://localhost:8000/trip/search?country=croatia&category=holiday&days=10%7C15
URL for page 2 looks like:
http://localhost:8000/trip/search?page=2
Giving error
`Undefined offset: 1`
I have used paginate before in laravel but haven't faced problem like this.