I have a form whose input values should be filled with the old values with Input::old()
:
{{ Form::open(['url' => '/directions', 'method' => 'get', 'class' => 'form-horizontal']) }}
<div class="form-group">
{{ Form::label('origin', 'Origin', ['class' => 'col-sm-2 control-label']) }}
<div class="col-sm-10">
{{ Form::text('origin', Input::get('origin'), ['class' => 'form-control', 'autocomplete' => 'off']) }}
</div>
</div>
<div class="form-group">
{{ Form::label('destination', 'Destination', ['class' => 'col-sm-2 control-label']) }}
<div class="col-sm-10">
{{ Form::text('destination', Input::get('destination'), ['class' => 'form-control', 'autocomplete' => 'off']) }}
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
{{ Form::submit('Buscar', ['class' => 'btn btn-default']) }}
</div>
</div>
{{ Form::close() }}
In the routes I created the view like this:
Route::get('/directions', function() {
$origin = Input::get('origin');
$destination = Input::get('destination');
$url = "http://maps.googleapis.com/maps/api/directions/json?origin=" . $origin . "&destination=" . $destination . "&sensor=false";
$json = json_decode(file_get_contents(str_replace(" ", "%20", $url)), true);
$result = var_export($json, true);
return View::make('home.index')->with('directions', $result);
});
However, it seemed like the old input values were not passed to the view, so I changed the last line with this:
return Redirect::to('/')->withInput()->with('directions', $result);
Now Input::old()
keeps without getting the old input values, but Input::get()
does. Also, the variable directions
is detected as null in the view.
What am I doing wrong? Why aren't the values passed to the views?
If your last line is:
return Redirect::to('/')->withInput()->with('directions', $result);
then in your views you can have access to every input parameter using:
Input::old('parameter');
In order to get access to the passed directions
you have to use Session
:
{{ Session::get('directions') }}
If you don't like this and you want to use your very first choice:
return View::make('home.index')->with('directions', $result);
in order to have access to your inputs in your views, right before that add:
Input::flash(); or Input::flashOnly('origin', 'destination');
now in your views Input::old('origin')
shall work as wanted.