What does $arr_answers return exactly? Can you post that here?
Can't store data from multiple choice questions. (Illegal string offset 'answer')
Im having some difficulty trying to store the results from my multiple choice questions. So I have have 2 tables [questions] and [answers]. All the questions are stored in the question table and when I want to display then I have this
`{{!!Form::open(...)!!}}
@foreach ($questions as $question)
<div>{{$question->question}}</div>
<div class="form-group">
{!! Form::label('answer', 'Score') !!}
{{ Form::selectRange(' answer[]', 1, 10) }}
{{ Form::hidden('question_id[]', $question->id) }}
{{ Form::hidden('user_id[]', $userId) }}
</div>
@endforeach
{{!!Form::submit(...)!!}}
{{!!Form::close()!!}}`
Ok so im also passing the question_id and the user_id which is the same for all the questions. My controller looks like this.
`public function store(Request $request)
{
$arr_answers = $request->all();
foreach ($arr_answers as $row) {
QuestionResults::create([
'answer' => $row['answer'],
'question_id' => $row['question_id'],
'user_id' => $row['user_id'],
]);
}
return "working";`
$arr_answers returns 3 arrays, the answer, the question_id and the user_id, all with the correct information, so my thought process is the foreach loop should return an int for each value but unfortunately its not playing ball and returning "Illegal string offset".
Please could someone point me in the direction of a solution or a better method to doing this.
I found a solution,
foreach ($arr_answers['answer'] as $key => $row) {
QuestionResults::create([
'answer' => $row,
'question_id' => $arr_answers['question_id'][$key],
'user_id' => $arr_answers['user_id'][$key],
]);
}
Please or to participate in this conversation.