在Laravel中更新阵列数据

时间:2018-03-04 06:30:43

标签: php laravel

我已经创建了一个可以提交数组数据的表单,但是当用户想要编辑表单数据时,我在更新数组时遇到了问题。

这是在视图中

@foreach($prescriptions as $prescription)

  <input type="hidden" name="prescript_id[]" value="{!! $prescription->prescript_id !!}"> 
    <tr> 
       <td><input class="form-control" name="drugname[]" type="text" value="{{ $prescription->drugname }}"></td>
       <td><input class="form-control" id="drugdosage" name="drugdosage[]" type="text" value="{{ $prescription->drugdosage }}"></td>
       <td><input class="form-control" id="frequency" name="frequency[]" type="text" value="{{ $prescription->frequency }}"></td>
      <td><input class="form-control" id="notes" name="notes[]" type="text" value="{{ $prescription->notes }}"></td>
      <td>RM <input class="form-control" id="price" name="price[]" placeholder="0.00" type="text" value="{{ $prescription->price }}"></td>
      <td><a class="btn del">-</a></td>
 </tr>@endforeach

这是在我的控制器中更新数据

public function update($consultid, Request $request)
{

        $docadvice = $request->get('docadvice');

        $drugnames = $request->get('drugname');
        $drugdosage = $request->get('drugdosage');
        $frequency = $request->get('frequency');
        $notes = $request->get('notes');
        $price = $request->get('price');

        $prescriptid = $request->get('prescript_id');

        $prescription = Prescription::where('prescript_id', '=', $prescriptid)->first();

            $count_items = count($drugnames);

        for($i = 0; $i<$count_items; $i++)
        {

            Prescription::where('prescript_id', '=', $prescriptid)->update([

            'drugname' => $drugnames[$i],
            'drugdosage' => $drugdosage[$i],
            'frequency' => $frequency[$i],
            'notes' => $notes[$i],
            'price' => $price[$i],
            'doc_advice' => $docadvice,

            ]);

        }

        return redirect(action('Doctor\PrescriptionController@edit', $consultation->consult_id))->with('status', 'The prescription has been updated!');
}

当我尝试运行代码时,只需要插入最后一个值而不是更新所有行值(假设有两行,当我插入

第1行:药物1 第2行:药物2

两行都只会更新药物2。 第1行:药物2 第2行:药物2

根据要求,我检查dd($request->toArray());所有插入的数据都已读取

帮助我了解如何更新两行和您的信息,我创建了一个动态表来添加新行,但它不会在数据库中添加新行,而只是为存在的行更新最后一个值。谢谢。

这是表处方 enter image description here

我取出隐藏字段并更改为文本 enter image description here

<div class="table-responsive">
            <table class="tblform table table-bordered table-striped table-hover">
                <thead>
                    <tr>

                        <th>Drug Name</th>
                        <th>Drug Dosage</th>
                        <th>Frequency</th>
                        <th>Notes</th>
                        <th>Price</th>
                        <th>Delete</th>
                    </tr>
                </thead>
                <tfoot>
                   <tr>

                        <th>Drug Name</th>
                        <th>Drug Dosage</th>
                        <th>Frequency</th>
                        <th>Notes</th>
                        <th>Price</th>
                        <th>Delete</th>
                    </tr>
                </tfoot>
                <tbody>

               @foreach($prescriptions as $prescription)

               <input type="text" name="prescript_id" value="{!! $prescription->prescript_id !!}"> 

              <tr> 
                <td>
                    <input class="form-control" name="drugname[]" type="text" value="{{ $prescription->drugname }}">
                  </td>
                  <td>
                    <input class="form-control" id="drugdosage" name="drugdosage[]" type="text" value="{{ $prescription->drugdosage }}">
                  </td>
                  <td>
                    <input class="form-control" id="frequency" name="frequency[]" type="text" value="{{ $prescription->frequency }}">
                  </td>
                  <td>
                    <input class="form-control" id="notes" name="notes[]" type="text" value="{{ $prescription->notes }}">
                  </td>
                  <td>
                    RM <input class="form-control" id="price" name="price[]" placeholder="0.00" type="text" value="{{ $prescription->price }}">
                  </td>
                  <td><a class="btn del">-</a></td>
                </tr>
               @endforeach

                </tbody>
          </table>



<div class="btn-toolbar">
                       <button class="btn btn-success pull-right" type="submit">Save</button>
                       <button class="btn pull-right" type="reset">Reset</button>
                    </div>
              </div>

             <button type="button" class="add btn btn-info">Add New Row</button>
          </div>
        </div>
      </div>
      </form>
    </section>




 <table style="display:none" class="table table-bordered table-striped table-hover" id="prototype">
  <tr> 
     <td>
      <input class="form-control" name="drugname[]" type="text">
    </td>
    <td>
      <input class="form-control" id="drugdosage" name="drugdosage[]" type="text">
    </td>
    <td>
      <input class="form-control" id="frequency" name="frequency[]" type="text">
    </td>
    <td>
      <input class="form-control" id="notes" name="notes[]" type="text">
    </td>
    <td>
      RM <input class="form-control" id="price" name="price[]" placeholder="0.00" type="text">
    </td>
  <td><a class="btn del">-</a></td>
</tr></table>

2 个答案:

答案 0 :(得分:0)

正如您所看到的,$prescriptid是一个数组,因此您必须在编写$drugnames之类的循环中编写它,例如`$ prescriptid [$ i]``

答案 1 :(得分:0)

请试试这个:

 public function update($consultid, Request $request) {

        $docadvice = $request->get('docadvice');

        $drugnames = $request->get('drugname');
        $drugdosage = $request->get('drugdosage');
        $frequency = $request->get('frequency');
        $notes = $request->get('notes');
        $price = $request->get('price');

        $prescriptid = $request->get('prescript_id');

        $prescription = Prescription::where('prescript_id', '=', $prescriptid)->first();

            $count_items = count($drugnames);

        for($i = 0; $i<$count_items; $i++)
        {

            $pres = Prescription::where('prescript_id', $prescriptid[$i])->first();

            $pres->update([
            'drugname' => $drugnames[$i],
            'drugdosage' => $drugdosage[$i],
            'frequency' => $frequency[$i],
            'notes' => $notes[$i],
            'price' => $price[$i],
            'doc_advice' => $docadvice,
            ]);

        }

        return redirect(action('Doctor\PrescriptionController@edit', $consultation->consult_id))->with('status', 'The prescription has been updated!'); }