Laravel pagination get variables

notforever picture notforever · Mar 4, 2014 · Viewed 10.5k times · Source

I have a page that list apartments depending on book dates like this

mypage.com/finder?date-from=2011-03-04&date-to=2011-03-12

Everything is right, I am getting the date-from and date-get from the url and searching the database with those values. The problem is when I paginate and I click to go to another page the url changes to.

mypage.com/finder?page=9 

and get an error Value must be provided

The correct url must be

mypage.com/finder?date-from=2011-03-04&date-to=2011-03-12&page=9

I am using paginate at the controller and $searchResult->links(); to generate the links

What can I do pass the date values from page to page so the pagination works?

Thanks

Answer

Joseph Silber picture Joseph Silber · Mar 4, 2014

If you want to tack on existing query string data, use this:

$searchResult->appends(array(
    'date-from' => Input::get('date-from'),
    'date-to'   => Input::get('date-to'),
));

Read the docs: Appending To Pagination Links.


You can shorten that a little:

$searchResult->appends( Input::only('data-from', 'date-to') );

which ends up being the same thing.