如何在Laravel 5.4中插入多个表格

时间:2019-12-05 01:48:37

标签: php laravel

我在输入多种形式时遇到问题,就我而言,结果数组就是这样

{
approval_activities": null,
  "remark_id": [
    "9",
    "10"
  ],
  "name_remark": [
    "Capex",
    "Asset"
  ],
  "label_remark": [
    "Capex",
    "Asset"
  ],
  "description_remark": [
    "Capex",
    "Asset"
  ],
  "filter_logic_remark": [
    null,
    null
  ],
  "default_remark": [
    "Capex",
    "Asset"
  ],
  "weight_remark": [
    "5",
    "1"
  ]

我尝试使用foreach将其插入到其他表中,但出现错误消息数组到字符串转换

$ asset = Asset :: create([,'approval_activities'=> $ request-> approval_activities]); $ arr [] = $ request-> all();

      foreach ($arras $req) { //remark
        $i = 0;
        $pret = [
          'asset_id'        => $asset->id,
          'remark_id'       => $req[$i]['remark_id'],
          'name'            => $req[$i]['name_remark'],
          'label'           => $req[$i]['label_remark'],
          'description'     => $req[$i]['description_remark'],
          'filter_logic'    => $req[$i]['filter_logic_remark'],
          'default'         => $req[$i]['weight_remark']
        ];
        $i++;
      }
      RemarkAsset::create($pret);

这是我在刀片中的表单,在此代码中,我使用[]作为向控制器发送数组的名称,此代码的结果类似于上面的JSON。

{!! Form::label('approval_activities', 'Approval Jenis Kegiatan', ['class' => 'col-sm-2 control-label']) !!}
<div class="col-sm-4 {{ $errors->has('approval_activities') ? 'has-error' : ''}}">
<select id="approval_activities" name="approval_activities" class="form-control select2_demo_1"
data-placeholder="Select Approval Jenis Kegiatan">
<option value="">Select Approval Jenis Kegiatan</option>
<option value="Aktivasi">Aktivasi</option>
</select> {!! $errors->first('approval_kegiatan', '<p class="help-block">:message</p>') !!}
</div>
        <div class="form-group">
    {!! Form::label('name', 'Name', ['class' => 'col-sm-2 control-label']) !!}
    <div class="col-sm-4 col-xs-12 {{ $errors->has('name') ? 'has-error' : ''}}">
    {!! Form::text('name_remark[]', $item->name, ['class' => 'form-control','placeholder'=>'Name']) !!}
    {!! $errors->first('name', '<p class="help-block">:message</p>') !!}
    </div>

    {!! Form::label('label', 'Label', ['class' => 'col-sm-2 control-label f-w-none']) !!}
    <div class="col-sm-4 col-xs-12 {{ $errors->has('label') ? 'has-error' : ''}}">
    {!! Form::text('label_remark[]', $item->name, ['class' => 'form-control','placeholder'=>'Label']) !!}
    {!! $errors->first('label', '<p class="help-block">:message</p>') !!}
    </div>
    </div>

我的期望是在表资产中插入$asset仅具有1条记录,并将该注释插入到表注释中,如果我上面有两个remark_id或其他类似JSON的表,则该表注释中的记录为两个。 >

1 个答案:

答案 0 :(得分:0)

如果数组如下所示,则$req[$i]['name_remark']将起作用,其中每个name_remark都有一个$i

{
    "name_remark": "Capex"
},
{
    "name_remark": "Asset"
}

对于您而言,我相信您希望使用$req['name_remark'][$i]