samedi 4 juillet 2020

Array insert database with foreach loop in laravel

View code

@foreach($questions as $question)
<input type="hidden" name="question[]" value="">
<textarea name="answers[]"></textarea>
@endforeach

Controller Code

public function finishExam(Request $request)
{
foreach ( $request->get('question') as $answer) {
     $answers[] = [
         'quiz_id' => $quiz->id,
         'user_id' => $user_record->id,
         'written_question_id' => $answer->question,
         'answers' => $answer->answers
         ];
    }
WrittenResult::insert($answers);
}

Error Showing when submission the form enter image description here

Array showing when print as var_dump or print_r

$items = array();
   foreach($request as $username) {
   $items[] = $username;
}
print_r($items);

enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire