在Laravel中更新阵列数据

问题描述 投票:2回答:3

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

这是在视图中

@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>
php laravel
3个回答
0
投票

正如你所看到的,$prescriptid是一个数组,所以你必须在循环中编写它,因为你编写了$drugnames,如`$ prescriptid [$ i]``


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');
       $count_items = count($drugnames);
       for($i = 0; $i<$count_items; $i++)
       {
            $prescription = Prescription::find($prescriptid[$i]);
            $prescription->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', $consultid))->with('status', 'The prescription has been updated!');
}

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!'); }
© www.soinside.com 2019 - 2024. All rights reserved.