简体   繁体   English

如何将具有多个字段的动态输入保存到数据库Laravel 5.7

[英]How can I save dynamic input with multiple fields to database Laravel 5.7

Im working on a project using laravel. 我正在使用laravel进行项目。 One function of the site is to save data from a dynamic input with four data; 该站点的一个功能是使用四个数据保存动态输入的数据; MObjective, MOutput, year, and kra_id which should be saved to database 应该保存到数据库的MObjective,MOutput,year和kra_id

i tried using this code to save; 我尝试使用此代码保存; ''' $objectives = $request->all(); '''$ goals = $ request-> all();

    foreach ($objectives as $key) {
        // dd($input);
        $input = objective::create(array(
            'MObjective' => $objectives->MObjective[$key],
            'MOutput' => $objectives->MOutput[$key],
            'year' => $objectives->year,
            'kra_id' => $objectives->kra_id

        ));
      }

''' I also tried to use for loop however i got some error like; '''我也尝试使用for循环,但是我得到了一些错误; Trying to get property 'MObjective' of non-object 试图获得非对象的属性'MObjective'

this is may controller; 这可能是控制者;

  $objectives = $request->all();
        foreach ($objectives as $key) {
            // dd($input);
            $input = objective::create(array(
                'MObjective' => $objectives->MObjective[$key],
                'MOutput' => $objectives->MOutput[$key],
                'year' => $objectives->year,
                'kra_id' => $objectives->kra_id

            ));
          }

my blade file; 我的刀片文件;

  <form method="POST" action="{{action('ObjectiveController@store')}}">
              {{csrf_field()}}
              <div class="form-group">
                <input type="hidden" name="year" id="year" value="{{now()->year}}" class="form-control">
                <input type="hidden" class="form-control" id="kra_id" name="kra_id">
              </div>
              <div class="form-group">
                <label for="message-text" class="col-form-label">Major Objective:</label>
                <textarea class="form-control" id="MObjective" name="MObjective"></textarea>
              </div>
              <div class="form-group">
                <label for="message-text" class="col-form-label">Major Output:</label>
                <textarea class="form-control" id="MajorOutput" name="MOutput"></textarea>
              </div>
              <div class="modal-footer">
                <b><i>Note:</i></b>
                <small>Add Major Objective and Output for selected KRA only</small>
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-success">Add</button>
              </div>
            </form>

my script ''' 我的剧本'''

var i=0;
  $('.addRow').on('click', function(){
    i++;
    addRow();
  });
  function addRow()
  {
    var tr='<div class="row">'+'<div class="col-6"><textarea type="text" class="form-control" placeholder="Enter the Major Objective no. '+i+' "name="MObjective['+i+']"></textarea></div>'+
      '<div class="col-6"><textarea type="text" class="form-control" name="MOutput['+i+']" placeholder="Enter Major Output for the objective no. '+i+'"></textarea></div>'+
      '</div><br>';
    $('form').append(tr);
  }

''' “””

i just need to save the 4 data within one db table; 我只需要在一个db表中保存4个数据; MObjective, MOutput, year, and kra_id. MObjective,MOutput,year和kra_id。 I hope someone could help me. 我希望有人可以帮助我。 thank you in advance 先感谢您

在你的控制器中,如果没有错误,$ objective应该是一个数组而不是一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM