Home > Net >  Laravel : How save array in database?
Laravel : How save array in database?

Time:11-16

I need your help because I can't save array from form in the pivot table. If someone can show me how because this is going to drive me crazy.

So I have a exam form with input number in foreach and which has a dynamic name. And I have two total results to.

This is my data form : Screen of DD

I want save my input number question in pivot table and the results on another table. For the result it's ok but it's for save in pivot table I have a problem.

Thanks to help me.

In my controller I make this :

$data = $request->all();
foreach($data as $question_id => $answer_id){
            $result = new PratiqueReponse();
            $result->question_id = $question_id;
            $result->answer_id = $answer_id;
            $result->save();
        }

But not working.

CodePudding user response:

You did not share much information, so i can only guess but i think you should save to db like this:

$data = $request->all();
foreach($data['questions'] as $question_id => $answer_id){
    $result = new PratiqueReponse();
    $result->question_id = $question_id;
    $result->answer_id = $answer_id;
    $result->save();
}

you need to loop trough questions, not the whole array.

  • Related