danny620's avatar

Re-populate Form with Get Request

Hi, I cant find away to re-populate my form with get parameters I have a url like

http://localhost:8000/courses?search=test&category=2&location=&lat=53.559003&lng=-2.077371&radius=2

and my form for the page is as you can see I'm using the "old('radius')" but its not picking up on the get parameter also the pagination is not keeping the parameters too.

{{ Form::select('radius', [ '2' => 'Distance: 2 mi', '5' => 'Distance: 5 mi', '15' => 'Distance: 15 mi', '20' => 'Distance: 20 mi', '30' => 'Distance: 30 mi', '40' => 'Distance: 40 mi', '60' => 'Distance: 60 mi',], old('radius'), ['class' => 'chosen-select-no-single', 'data-placeholder' => 'Choose Distance'] ) }}

0 likes
3 replies
spekkionu's avatar

The old helper function pulls flashed data from the session that is set when redirecting with a withInput .

You can pass the get parameters to your view from your controller and pass these variables as a second parameter to the old function.

danny620's avatar

is there no better way to do this? what about the pagination parameters

spekkionu's avatar

Laravel is designed to be using route parameters rather than the query string for urls. Using forms with GET methods and urls with query strings breaks this convention and means you have to do a bit more yourself. You can pull the query string using $request->query().

For the pagination you will use the appends method to add any query string parameters you want to the urls. This can be called either in the controller or the view itself.

{!! $users->appends(['sort' => 'votes'])->links() !!}

Please or to participate in this conversation.