-4

I have form with multiple dynamically created HTML inputs, data which are collected from inputs I want to store in database with the createMany() method, but currently only data from 'answers' inputs are stored in database.

After some time I realized that for createMany() I need to have merged data into one array. But i don't know how to manage that. If anyone can help me i would appreciate.

enter image description here

blade.php

<div class="row justify-content-center">   
    <div class="col-lg-8">
        <div class="card">
            <div class="card-body">
              <h3 class="card-title">Create Question</h3>
                  <hr class="m-t-0 m-b-40">
                   {!! Form::model(array(), ['route' => ['question.store', $questionnaire->id], 'role' => 'form', 'class' => 'form-material m-t-40']) !!}
                    <div class="row">
                        <div class="col-md-12 col-lg-12 col-xl-12">
                        {!!($errors->has('question')) ? '<div class="form-group has-error">' : '<div class="form-group">'!!}
                          <label>{{trans('core::commander.question')}}</label>
                          {!! Form::text('question[question]', old('name'), array('class'=>'form-control')) !!}
                        </div>
                     </div>        
                   </div>             


                <h3 class="card-title">Create Answers</h3>
                <hr class="m-t-0 m-b-40">

                    <div id="example">
                       <div class="row" id="answer_field">
                        <div class="col-md-5 col-lg-5 col-xl-5">
                          <label>Answer</label>
                          {!! Form::text('answers[][answer]', old('name'), array('class'=>'form-control')) !!}
                          </div>

                        <div class="col-md-5 col-lg-5 col-xl-5">   
                          <label>Value:</label>
                           <input type="number" name="answer_value[][value]" min="0" max="10" class="form-control">
                        </div>   
                           <a class="btn btn-primary add_more_button">Add More Fields</a>

                        </div>
                     </div> 




                    <div class="col-md-6 col-lg-2 col-xl-2">
                      <button class="btn btn-block btn-default btn-md-6" type="submit" style="margin-top: 11%;"><i class="fa fa-check"></i> {{trans('core::commander.save')}}
                      </button>
                    {!! Form::close() !!} 
                  </div>
            </div>
        </div>
    </div>





@endsection  


@section('view-scripts')

<script>


   var i=1;  
      $('.add_more_button').click(function(){  
           i++;  


           $('#example').append('<div class="row" id="answer_field'+i+'"><div class="col-md-5 col-lg-5 col-xl-5"><label>Answer</label><input type="text" name="answers[][answer]" class="form-control"/></div><div class="col-md-5 col-lg-5 col-xl-5"><label>Value</label><input type="number" name="answer_value[][value]" min="0" max="10" class="form-control"/></div><a href="#" id="'+i+'" class="input_fields_container_part" style="margin-left:10px;">Remove</a></div></div>'); //add input field
      }); 
$(document).on('click', '.input_fields_container_part', function(){  
           var button_id = $(this).attr("id");   
           $('#answer_field'+button_id+'').remove();


      });  
</script>

Controller.php

 public function storeQuestion(Request $request, $id){

            $data= request()->all();




            $questionnaire=Questionnaire::with('questionnaireQuestions')->where('id', '=', $id)->first();
            $question=$questionnaire->questionnaireQuestions()->create($data['question']);
            $question->questionAnswers()->createMany($data['answers']);


             return view('questionnaire.questions.createQuestions')->with('questionnaire', $questionnaire);

      }  

Array goal

array:4 [▼
      "_token" => "eRzBm87dqPbzOjN1I8k6lD1cttHhNbeEUL0HUTCQ"
      "question" => array:1 [▶]
      "answers" => array:2 [▼
        0 => array:1 [▼
          "answer" => "dasd"
        ]
        1 => array:1 [▼
          "answer" => "asda"
        ]
      ]
      "answer_value" => array:2 [▼
        0 => array:1 [▼
          "value" => "2"
        ]
        1 => array:1 [▼
          "value" => "3"
        ]
      ]
    ]


array:4 [▼
      "_token" => "eRzBm87dqPbzOjN1I8k6lD1cttHhNbeEUL0HUTCQ"
      "question" => array:1 [▶]
      "answers" => array:2 [▼
        0 => array:1 [▼
          "answer" => "dasd",
          "value" => "2"
        ]
        1 => array:1 [▼
          "answer" => "asda",
          "value" => "3"
        ]
      ]

    ]
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Nenad
  • 323
  • 2
  • 6
  • 21
  • 1
    already solved https://stackoverflow.com/questions/6535444/combine-two-arrays – Kamlesh Paul Jan 29 '20 at 11:05
  • The result of this function is : https://imgur.com/a/15OscnB , but i want that my array structure looks like here : https://imgur.com/a/tRsRpPw – Nenad Jan 29 '20 at 11:20

1 Answers1

1

Have you tried array_merge_recursive function? https://www.php.net/manual/en/function.array-merge-recursive.php

Upd.

Can you try this?

foreach($arr['answers'] as $key => $value){
    $arr['answers'][$key]['value'] = $arr['answer_value'][$key]['value'];
    unset($arr['answer_value'][$key]['value']);
}
webprogrammer
  • 2,393
  • 3
  • 21
  • 27