I can't manage to repopulate a Select field input after a query, using Laravel 4:
// Route
Route::get('blog', 'BlogController@getPosts');
// BlogController
public function getPosts()
{
$posts = Post::where('category_id', Input::get('category'))->paginate(25);
$categories = Category::lists('title', 'id');
return View::make('blog', compact('categories', 'posts'));
}
// Blog view
{{ Form::open('method' => 'get', 'id' => 'form-search') }}
{{ Form::select('category', $categories, Input::old('category')) }}
{{ Form::close() }}
I managed to make it work this way, but it's not the best practice
<select name="category" id="category">
<option value="1" {{ (Input::get('category') == 1) ? 'selected="selected"' : null }}>Category 1</option>
<option value="2" {{ (Input::get('category') == 2) ? 'selected="selected"' : null }}>Category 2</option>
<option value="3" {{ (Input::get('category') == 3) ? 'selected="selected"' : null }}>Category 3</option>
</select>
I think the Input::old('category')
doesn't work because it is a GET request, am I right? Is there any workarounds?
Update : I finally made it work using Input::get()
instead of Input::old()
:
{{ Form::select('category', $categories, Input::get('category')) }}
Input::old()
is a default value in the event that old is empty.Input::old('category', null);
for instance. – Ohgodwhy Sep 24 '14 at 20:39/?category=1
– Phmarc Sep 24 '14 at 20:45