Search code examples
phplaravellaravel-5laravelcollectivelaravel-form

Laravel: How to get the old selected value from the post request


I have laravel blade form with choose option:

{!! Form::select('id', [''=>'Choose:'] + $name, null, ['class'=>'form-control']) !!}

but I need this Choose option only first time in that page, after submission I need to stay last value that I used.

I tried like this with old function:

{!! Form::select(
    'id', 
    ['' => 'Choose:'] + $name, 
    old('id', null), 
    ['class' => 'form-control]
) !!}

but this doesn't work. Also, I google old function with @if statement like this:

@if( old('id') == $name) selected="selected" @endif

but also after submit goes back to Choose...


Solution

  • If you are using Laravel Collective form then

    {!! Form::select('id', [''=>'Choose:'] + $name, null, ['class'=>'form-control']) !!}
    

    should be enough.

    Make sure you open form also using

    {!! Form::open(...
    

    and that you are using standard validation (otherwise make sure you redirect back using

    ->withInput()