Home > other >  How to get old input value from name input array laravel
How to get old input value from name input array laravel

Time:05-09

I have this code

@foreach($questions as $question_name => $question_details)
                            <h5>{{ $question_name }}</h5>
                            @foreach($question_details as $question_id => $question_description)
                                <p >{{ $question_description }}</p> 
                                <input type="hidden" name="question_id[]" value="{{ $question_id }}"/>
                                <textarea name="answers[]"  cols="30" rows="2" placeholder="Your answer here.."></textarea>
                            @endforeach
                        @endforeach

And when I submit a form with some empty values, the answers that I got also get deleted if some error occurs I tried this to get back my old inputs but it didn't bring me anything

I tried

<textarea name="answers[]"  cols="30" rows="2" placeholder="Your answer here..">{{ old('answers') }}</textarea>

<textarea name="answers[]"  cols="30" rows="2" placeholder="Your answer here..">{{ old('answers[]') }}</textarea>

<textarea name="answers[]"  cols="30" rows="2" placeholder="Your answer here..">{{ old('answers.0') }}</textarea>

But nothing really succeed to bring me back the old values that I typed can someone help me with that?

CodePudding user response:

<textarea name="answers[]" 
     cols="30" rows="2" 
    placeholder="Your answer here..">

    {{ old('answers.' . $loop->index) }}

</textarea>

CodePudding user response:

When you submit a form that has a HTML array (name="answers[]"), it will be converted to a proper PHP array in the backend. This also means that the values will be indexed like a PHP array is always indexed. If you have three rows, even without values, you can expect it to look like so:

dd($request->input('answers');
/* Output: [
    0 => null,
    1 => null,
    2 => null,
]; */

When you then fetch old('answers'), you will get an array. If you want the second answer, you will have to fetch old('answers.1').

If you want the structure to be more clear and dynamic, you could use your question ID as index parameter:

@foreach($question_details as $question_id => $question_description)
    <p >{{ $question_description }}</p> 
    <textarea name="answers[{{$question_id}}]"  cols="30" rows="2" placeholder="Your answer here...">{{old("answers.{$question_id}")}}</textarea>
@endforeach

Now your array will be keyed by your question IDs. If, however, you don't want to do that, you can also key by loop index:

@foreach($question_details as $question_id => $question_description)
    <p >{{ $question_description }}</p> 
    <input type="hidden" name="question_id[{{$loop->index}}]" value="{{ $question_id }}"/>
    <textarea name="answers[{{$loop->index}}]"  cols="30" rows="2" placeholder="Your answer here...">{{old("answers.{$loop->index}")}}</textarea>
@endforeach

You can read more about the $loop variable here

  • Related