1
votes

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.

2
A debug stack trace would be helpful.GiamPy
can you call localhost:8000/trip/… without errors?Ahmed Bebars
Yes, it does take me to page 2 with required data.Zachary Dale
Try my answer on pagination. it is dynamic link here stackoverflow.com/questions/41953914/…Vikash

2 Answers

1
votes

The problem here, when you go to the next page you don't carry over the query string data so you need to pass this data to your view and you can use the following

{{ $data->appends(Request::only('country','categorry','days'))->links() }}

This should fix it

0
votes

You need to use the append() method to append data to pagination links:

{{ $data->appends([
       'country' => 'croatia', 
       'category' => 'holiday',
       'days' => '10%7C15'
   ])->links() }}

https://laravel.com/docs/5.3/pagination#displaying-pagination-results