I am trying to get the ids for the four answers for a single question from a Laravel Edit form.
Here is my form view blade code:
@php $i=0; @endphp
@foreach($question->answers as $answer)
@php $i++; @endphp
<div class="form-group">
<div class="col-lg-8">
{!!Form::label('Answeroption'.$i, 'Answeroption'.$i)!!}
{!! Form::text('answeropt'.$i,$answer->answeropt, ['class'=>'form-control', 'id' => $answer->id]) !!}
</div>
</div>
@endforeach
I want to get this 'id'=> $answer->id in the QuestionController, Edit method. This piece of code is in the question edit form with 4 answer options. Each answer is stored in answer table with different its id linked to the main question id. I want to get this answerid,so that I can store the edited answers.
I tried to do getIdAttribute but it is not working.
Here is the screenshot of how my screen looks. Each option has an entry in answer table with question_id linked to it as HasMany relationship.
Screenshot of the Edit Question Form
You would be better off using an array in your markup. Then you will have the ids to reference upon post. So this might look something like:
{!! Form::text('answeropt['. $answer->id . ']',$answer->answeropt, ['class'=>'form-control', 'id' => $answer->id]) !!}
Then in your controller, you can loop over the array and utilize the id. So that might look something like:
foreach($request->get('answeropt', []) as $answerId => $answer) {
// do things with the answer id and the answer text
}
I think this is what you are after? Please let me know if this is clear, or if you meant something else.