简体   繁体   中英

Update Array Data In Laravel

I have already done creating a form that can submit array data, however I am having problem on updating the array when user wants to edit the form data.

Here is in the view

@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

And here is in my controller on updating the data

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!');
}

When I try to run the code, it is only takes the last values i have inserted not all values of rows updated (Assume there are two rows, when I inserted

row 1: Drug 1 row 2: Drug 2

Only Drug 2 will be updated on both rows. row 1: Drug 2 row 2: Drug 2

Upon request I check on dd($request->toArray()); all data inserted are read

Help me on how to update for both rows and for your information, I have created a dynamic table to add new row but it won't add the new row in database but only takes the last value to be updated for rows existed. Thank you.

This the table prescription在此处输入图片说明

I take out the hidden field and change to text在此处输入图片说明

<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>
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!');
}

Please try this:

 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!'); }

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

$prescription = Prescription::findOrFail($prescriptid)->get();
$count_items = count($drugnames);

for($i = 0; $i<$count_items; $i++)
{
    $prescription[$count_items]->update([
        'drugname' => $drugnames[$i],
        'drugdosage' => $drugdosage[$i],
        'frequency' => $frequency[$i],
        'notes' => $notes[$i],
        'price' => $price[$i],
        'doc_advice' => $docadvice,
    ]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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