title: Update Array Data In Laravel
body: <p>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.</p>
  
  <p>Here is in the view</p>
  
  <pre><code>@foreach($prescriptions as $prescription)
  
    &lt;input type="hidden" name="prescript_id[]" value="{!! $prescription-&gt;prescript_id !!}"&gt; 
      &lt;tr&gt; 
         &lt;td&gt;&lt;input class="form-control" name="drugname[]" type="text" value="{{ $prescription-&gt;drugname }}"&gt;&lt;/td&gt;
         &lt;td&gt;&lt;input class="form-control" id="drugdosage" name="drugdosage[]" type="text" value="{{ $prescription-&gt;drugdosage }}"&gt;&lt;/td&gt;
         &lt;td&gt;&lt;input class="form-control" id="frequency" name="frequency[]" type="text" value="{{ $prescription-&gt;frequency }}"&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input class="form-control" id="notes" name="notes[]" type="text" value="{{ $prescription-&gt;notes }}"&gt;&lt;/td&gt;
        &lt;td&gt;RM &lt;input class="form-control" id="price" name="price[]" placeholder="0.00" type="text" value="{{ $prescription-&gt;price }}"&gt;&lt;/td&gt;
        &lt;td&gt;&lt;a class="btn del"&gt;-&lt;/a&gt;&lt;/td&gt;
   &lt;/tr&gt;@endforeach
  </code></pre>
  
  <p>And here is in my controller on updating the data</p>
  
  <pre><code>public function update($consultid, Request $request)
  {
  
          $docadvice = $request-&gt;get('docadvice');
  
          $drugnames = $request-&gt;get('drugname');
          $drugdosage = $request-&gt;get('drugdosage');
          $frequency = $request-&gt;get('frequency');
          $notes = $request-&gt;get('notes');
          $price = $request-&gt;get('price');
  
          $prescriptid = $request-&gt;get('prescript_id');
  
          $prescription = Prescription::where('prescript_id', '=', $prescriptid)-&gt;first();
  
              $count_items = count($drugnames);
  
          for($i = 0; $i&lt;$count_items; $i++)
          {
  
              Prescription::where('prescript_id', '=', $prescriptid)-&gt;update([
  
              'drugname' =&gt; $drugnames[$i],
              'drugdosage' =&gt; $drugdosage[$i],
              'frequency' =&gt; $frequency[$i],
              'notes' =&gt; $notes[$i],
              'price' =&gt; $price[$i],
              'doc_advice' =&gt; $docadvice,
  
              ]);
  
          }
  
          return redirect(action('Doctor\PrescriptionController@edit', $consultation-&gt;consult_id))-&gt;with('status', 'The prescription has been updated!');
  }
  </code></pre>
  
  <p>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 </p>
  
  <p>row 1: Drug 1
  row 2: Drug 2</p>
  
  <p>Only Drug 2 will be updated on both rows.
  row 1: Drug 2
  row 2: Drug 2 </p>
  
  <p>Upon request I check on <code>dd($request-&gt;toArray());</code> all data inserted are read</p>
  
  <p>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.</p>
  
  <p>This the table prescription 
  <a href="https://i.stack.imgur.com/R0WM3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R0WM3.png" alt="enter image description here"></a></p>
  
  <p>I take out the hidden field and change to text
  <a href="https://i.stack.imgur.com/gWn4j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gWn4j.png" alt="enter image description here"></a></p>
  
  <pre><code>&lt;div class="table-responsive"&gt;
              &lt;table class="tblform table table-bordered table-striped table-hover"&gt;
                  &lt;thead&gt;
                      &lt;tr&gt;
  
                          &lt;th&gt;Drug Name&lt;/th&gt;
                          &lt;th&gt;Drug Dosage&lt;/th&gt;
                          &lt;th&gt;Frequency&lt;/th&gt;
                          &lt;th&gt;Notes&lt;/th&gt;
                          &lt;th&gt;Price&lt;/th&gt;
                          &lt;th&gt;Delete&lt;/th&gt;
                      &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tfoot&gt;
                     &lt;tr&gt;
  
                          &lt;th&gt;Drug Name&lt;/th&gt;
                          &lt;th&gt;Drug Dosage&lt;/th&gt;
                          &lt;th&gt;Frequency&lt;/th&gt;
                          &lt;th&gt;Notes&lt;/th&gt;
                          &lt;th&gt;Price&lt;/th&gt;
                          &lt;th&gt;Delete&lt;/th&gt;
                      &lt;/tr&gt;
                  &lt;/tfoot&gt;
                  &lt;tbody&gt;
  
                 @foreach($prescriptions as $prescription)
  
                 &lt;input type="text" name="prescript_id" value="{!! $prescription-&gt;prescript_id !!}"&gt; 
  
                &lt;tr&gt; 
                  &lt;td&gt;
                      &lt;input class="form-control" name="drugname[]" type="text" value="{{ $prescription-&gt;drugname }}"&gt;
                    &lt;/td&gt;
                    &lt;td&gt;
                      &lt;input class="form-control" id="drugdosage" name="drugdosage[]" type="text" value="{{ $prescription-&gt;drugdosage }}"&gt;
                    &lt;/td&gt;
                    &lt;td&gt;
                      &lt;input class="form-control" id="frequency" name="frequency[]" type="text" value="{{ $prescription-&gt;frequency }}"&gt;
                    &lt;/td&gt;
                    &lt;td&gt;
                      &lt;input class="form-control" id="notes" name="notes[]" type="text" value="{{ $prescription-&gt;notes }}"&gt;
                    &lt;/td&gt;
                    &lt;td&gt;
                      RM &lt;input class="form-control" id="price" name="price[]" placeholder="0.00" type="text" value="{{ $prescription-&gt;price }}"&gt;
                    &lt;/td&gt;
                    &lt;td&gt;&lt;a class="btn del"&gt;-&lt;/a&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                 @endforeach
  
                  &lt;/tbody&gt;
            &lt;/table&gt;
  
  
  
  &lt;div class="btn-toolbar"&gt;
                         &lt;button class="btn btn-success pull-right" type="submit"&gt;Save&lt;/button&gt;
                         &lt;button class="btn pull-right" type="reset"&gt;Reset&lt;/button&gt;
                      &lt;/div&gt;
                &lt;/div&gt;
  
               &lt;button type="button" class="add btn btn-info"&gt;Add New Row&lt;/button&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;/form&gt;
      &lt;/section&gt;
  
  
  
  
   &lt;table style="display:none" class="table table-bordered table-striped table-hover" id="prototype"&gt;
    &lt;tr&gt; 
       &lt;td&gt;
        &lt;input class="form-control" name="drugname[]" type="text"&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;input class="form-control" id="drugdosage" name="drugdosage[]" type="text"&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;input class="form-control" id="frequency" name="frequency[]" type="text"&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;input class="form-control" id="notes" name="notes[]" type="text"&gt;
      &lt;/td&gt;
      &lt;td&gt;
        RM &lt;input class="form-control" id="price" name="price[]" placeholder="0.00" type="text"&gt;
      &lt;/td&gt;
    &lt;td&gt;&lt;a class="btn del"&gt;-&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;&lt;/table&gt;
  </code></pre>