2

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 enter image description here

I take out the hidden field and change to text 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>
14
  • Can you please take a snapshot of the form wrap in view page source code, so I can see if you are having the prescription values or not in the hidden prescription field. Commented Mar 4, 2018 at 8:35
  • And why do you have taken that hidden field as an array? Commented Mar 4, 2018 at 8:41
  • @HaiderAli how to upload image here, im sorry, first time for questioning in here. Yes, the prescription values are there for every row. I take it as array so that when I add a new row, it would update new row and insert prescription values for new data. I guess... :( Commented Mar 4, 2018 at 10:16
  • but each prescription record should only have 1 id so I guess there is no need of making it an array Commented Mar 4, 2018 at 10:18
  • And there is an option for uploading image in the question editor Commented Mar 4, 2018 at 10:19

4 Answers 4

1
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!');
}
Sign up to request clarification or add additional context in comments.

5 Comments

Error message appears Undefined variable: consultation at return redirect, and I changed that $consultation->consult_id to $prescription->consult_id (because both carries the same value), magically it change someone else's prescription :(.
Change $consultation->consult_id to $consultid
If I do that, it shows another error FatalThrowableError (E_ERROR) Call to a member function update() on null. It is highlighting on $prescription->update([
Do you have in Prescription model protected $primaryKey = 'prescript_id'; ?
No, I just wrote protected $guarded = ['id']; public function consultation() { return $this->hasOne('App\Consultation', 'consult_id'); }
1

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

2 Comments

Error message appears Undefined variable: consultation at return redirect.
Where is $consultation defined?
0

as you can see $prescriptid is an array, so you must write it in a loop as you wrote the $drugnames like `$prescriptid[$i]``

1 Comment

I changed it to Prescription::where('prescript_id', '=', $prescriptid[$i])->update([ but it keep updating the last value.
0
$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,
    ]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.