Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dalivan's avatar

store answer

Please help to fix it. Store function successfully loop for the answer but why question_id cannot loop. question_id only store last question_id

public function store(Request $request)
    {
        $user_id = Auth::id();
        foreach ((array)$request->answer as $value) {
            $answer = new Answer();
            $answer->user_id = $user_id;
            $answer->question_id = $request->question_id;
            $answer->answer = $value;
            $answer->save();
        }
    }
<form role="form" method="POST" action="{{ route('store-question') }}">
    @csrf
   <div class="box-body">
        @foreach ($question as $row)
            <input  type="text" name="question_id" value="{{ $row->id }}">
            <div class="row">
                <div class="col-md-12"> 
                    {{ $loop->iteration }}. {{ $row->question }}
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-check">
                        <label class="form-check-label">
                            <input type="radio" name="answer[{{$row->id}}]" value="A"> {{ $row->a }}
                        </label>
                    </div> 
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-check">
                        <label class="form-check-label">
                            <input type="radio" name="answer[{{$row->id}}]" value="B"> {{ $row->b }}
                        </label>
                    </div> 
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-check">
                        <label class="form-check-label">
                            <input type="radio" name="answer[{{$row->id}}]" value="C"> {{ $row->c }}
                        </label>
                    </div> 
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-check">
                        <label class="form-check-label">
                            <input type="radio" name="answer[{{$row->id}}]" value="D"> {{ $row->d }}
                        </label>
                    </div> 
                </div>
            </div>
            <hr>
        @endforeach
      
    </div>
    <div class="box-footer">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
</form>
0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@dalivan the problem is that this code:

<input  type="text" name="question_id" value="{{ $row->id }}">

in a loop will always be set to the last item in the collection.

So you might need to do this instead:

<input  type="text" name="question_id[$row->id]" value="{{ $row->id }}">

And in the controller:

foreach ((array)$request->answer as $rowId => $value) {
    $answer = new Answer();
    $answer->user_id = $user_id;
    $answer->question_id = $request->question_id[$rowId];
    $answer->answer = $value;
    $answer->save();
}

OR you could also just use the $rowId here as that's the question ID anyway, so just this should do it as well:

foreach ((array)$request->answer as $rowId => $value) {
    $answer = new Answer();
    $answer->user_id = $user_id;
    $answer->question_id = $rowId;
    $answer->answer = $value;
    $answer->save();
}

Let me know if it works :)

1 like
Dalivan's avatar

Thank you so much, its work perfectly.

Please or to participate in this conversation.