使用动态表laravel 5.6添加多个输入字段

时间:2018-11-08 05:02:12

标签: laravel laravel-5.6

我想存储来自具有多个字段的动态表中的数据。它的工作,如果我想存储一个表字段,但问题是(添加更多按钮)存储多个表字段。

这是我的控制器

if($request->type==='quiz'){
  //dd($request->all());     

     $quiz_id = DB::table('quizes')->insertGetId([
      'content_id' =>$content_id,
      'c_title'=>$request->c_title,
      'question'=>$request->question,
      'created_at'=>now(), 

  ]);

    //return $request->all();

    foreach ($request->c_title as $key => $value) {

      $new_answer->content_id=$content_id;
      $new_answer->quiz_id=$quiz_id;
      $new_answer->c_title =$request->c_title[$key];
      $new_answer->question=$request->question[$key];

      foreach ($request->answer as $key => $value) {
        $new_answer = new answer();
        $new_answer->answer =$request->answer[$key];
        $new_answer->correct=$request->correct[$key] ? 1 :0;
      }


  if(!$new_answer->save()){

  // handle error here
  }
   }
  return redirect()->route('content.index')->with('success', 'Content Successfully Recorded');
 }

}

这是我的 addcontent.blade.php 刀片

       else if(type==='quiz'){

        append_type=  '<div class="form-group">'+
                      '<label class="control-label col-md-4 col-sm-4 col-xs-12">Content Title:</label>'+
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+
                   '<input class="form-control" type="text" placeholder="Content Title" name="c_title">'+
                      '</div>'+
                    '</div>'+
                    '<div class="form-group">'+
                   '<label class="control-label col-md-4 col-sm-4'+
                    'col-xs-12">Question:</label>'+
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+
                      '<input class="form-control" type="text" placeholder="Question"'+
                       'name="question">'+
                      '</div>'+
                    '</div>'+
                    '<h3 align="center"> PLEASE ADD THE ANSWER  HERE</h3>'+ 
                    '<div class="form-group">'+
                     '<div class="col-sm-2">'+
                      '<input type="hidden" name="correct[]" value="1"  class="correct_1"/>'+
                      '<div class="checkbox" >'+
                  '<label><input type="checkbox" name="optradio"  value="1" id="option_1" checked>'+
                  'Correct?</label>'+
                     '</div>'+
                    '</div>'+           
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+

                   '<input class="form-control" type="text" placeholder=" Type the Answer"'+
                   'name="answer[]">'+
                      '</div>'+
                      '</div> '+
                     '<div class="form-group">'+
                     '<div class="col-sm-2">'+
                     '<input type="hidden" name="correct[]" value="0" class="correct_2"/>'+
                   '<div class="checkbox">'+
             '<label><input type="checkbox" name="optradio" value="2" id="option_2">Correct?</label>'+
                     '</div>'+
                    '</div>'+          
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+   
                        '<input class="form-control" type="text" placeholder="'+
                         'Type the Answer" name="answer[]">'+
                      '</div>'+
                    '</div>'+ 
                    '<div class="form-group">'+
                     '<div class="col-sm-2">'+
                      '<input type="hidden" name="correct[]" value="0" class="correct_3"/>'+
                        '<div class="checkbox">'+
                   '<label><input type="checkbox" name="optradio" value="3" id="option_3">'+
                   'Correct?</label>'+
                     '</div>'+
                    '</div>'+
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+
                        '<input class="form-control" type="text" placeholder="'+ 
                        'Type the Answer" name="answer[]">'+
                      '</div>'+
                    '</div>'+ 
                    '<div class="form-group">'+ 
                      '<div class="col-sm-2">'+
                      '<input type="hidden" name="correct[]" value="0" class="correct_4"/>'+
                      '<div class="checkbox">'+ 
                   '<label><input type="checkbox" name="optradio" value="4" id="option_4">'+
                   'Correct?</label>'+
                     '</div>'+
                      '</div>'+
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+
                        '<input class="form-control" type="text" placeholder="'+
                         'Type the Answer" name="answer[]">'+
                            '<button type="button" name="add" id="add" class="btn btn-success"'+
                       'style="margin-top:5px;">'+
                      'Add More</button></td>'+   
                      '</div>'+
                    '</div>'; 
                     $('#default_type_form').html(append_type);

            var html='<div class="append_quiz"><div class="form-group">'+
                      '<label class="control-label col-md-4 col-sm-4 col-xs-12">Content Title:</label>'+
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+
                   '<input class="form-control" type="text" placeholder="Content Title" name="c_title">'+
                      '</div>'+
                    '</div>'+
                    '<div class="form-group">'+
                   '<label class="control-label col-md-4 col-sm-4'+
                    'col-xs-12">Question:</label>'+
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+
                      '<input class="form-control" type="text" placeholder="Question"'+
                       'name="question">'+
                      '</div>'+
                    '</div>'+
                    '<h3 align="center"> PLEASE ADD THE ANSWER  HERE</h3>'+ 
                    '<div class="form-group">'+
                     '<div class="col-sm-2">'+
                      '<input type="hidden" name="correct[]" value="1"  class="correct_1"/>'+
                      '<div class="checkbox" >'+
                  '<label><input type="checkbox" name="optradio"  value="1" id="option_1" checked>'+
                  'Correct?</label>'+
                     '</div>'+
                    '</div>'+           
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+

                   '<input class="form-control" type="text" placeholder=" Type the Answer"'+
                   'name="answer[]">'+
                      '</div>'+
                      '</div> '+
                     '<div class="form-group">'+
                     '<div class="col-sm-2">'+
                     '<input type="hidden" name="correct[]" value="0" class="correct_2"/>'+
                   '<div class="checkbox">'+
             '<label><input type="checkbox" name="optradio" value="2" id="option_2">Correct?</label>'+
                     '</div>'+
                    '</div>'+          
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+   
                        '<input class="form-control" type="text" placeholder="'+
                         'Type the Answer" name="answer[]">'+
                      '</div>'+
                    '</div>'+ 
                    '<div class="form-group">'+
                     '<div class="col-sm-2">'+
                      '<input type="hidden" name="correct[]" value="0" class="correct_3"/>'+
                        '<div class="checkbox">'+
                   '<label><input type="checkbox" name="optradio" value="3" id="option_3">'+
                   'Correct?</label>'+
                     '</div>'+
                    '</div>'+
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+
                        '<input class="form-control" type="text" placeholder="'+ 
                        'Type the Answer" name="answer[]">'+
                      '</div>'+
                    '</div>'+ 
                    '<div class="form-group">'+ 
                      '<div class="col-sm-2">'+
                      '<input type="hidden" name="correct[]" value="0" class="correct_4"/>'+
                      '<div class="checkbox">'+ 
                   '<label><input type="checkbox" name="optradio" value="4" id="option_4">'+
                   'Correct?</label>'+
                     '</div>'+
                      '</div>'+
                      '<div class="col-md-8 col-sm-8 col-xs-12">'+
                        '<input class="form-control" type="text" placeholder="'+
                         'Type the Answer" name="answer[]">'+
                   '<button type="button" class="btn btn-danger remove_quiz" style="margin-top:5px;">'+
                      'X</button></td></div>';

      $('#add').click(function(e){
      $('#default_type_form').append(html);
      quiz_i++;
         //remove rows from the form
      $(".remove_quiz").on('click',function(){
      $(this).closest('div .append_quiz').fadeOut(function(){
      $(this).remove();
      });
         quiz_i--;
      });

      });
               var $radios = $('input[name="optradio"]');
                $radios.change(function(){

                  if($(this).val()==1){

                      $('#correct_1').val();
                       $('#correct_2').val(0);
                        $('#correct_3').val(0);
                         $('#correct_4').val(0);
                  }
                  else if($(this).val()==2){
                    $('#correct_2').val(1);
                     $('#correct_1').val(0);
                        $('#correct_3').val(0);
                         $('#correct_4').val(0);
                  } 
                    else if($(this).val()==3){
                    $('#correct_3').val(1);
                     $('#correct_1').val(0);
                      $('#correct_2').val(0);
                       $('#correct_4').val(0);
                  }
                 else if($(this).val()==4){
                    $('#correct_4').val(1);
                     $('#correct_2').val(0);
                      $('#correct_3').val(0);
                      $('#correct_1').val(0);
                  }


           console.dir($radios);

     });


      }

    });


  });
</script> 

如果有人可以提供帮助,将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

您应在输入字段中将c_title作为数组添加到c_titlec_title[]

在您的控制器中,您将在c_title属性中获得分配c_title数组的异常。所以您需要重新设计数据库表和代码

$quiz_id = DB::table('quizes')->insertGetId([
      'content_id' =>$content_id,
      'c_title'=>$request->c_title, // its an array not string
      'question'=>$request->question,
      'created_at'=>now(), 
  ]);



  foreach ($request->c_title as $key => $value) {

      $new_answer->content_id=$content_id;
      $new_answer->quiz_id=$quiz_id;
      $new_answer->c_title =$request->c_title[$key];
      $new_answer->question=$request->question[$key];

      foreach ($request->answer as $key => $value) {
        $new_answer = new answer();
        $new_answer->answer =$request->answer[$key];
        $new_answer->correct=$request->correct[$key] ? 1 :0;
      }