title: Laravel Update Single row attached with a foreach loop
body: <p><a href="https://i.stack.imgur.com/tReE1.png" rel="nofollow noreferrer">database structure here</a> i am creating a loan repayment system with laravel and whenever i repay a loan i want to update the &quot;principal repaid derived&quot; and &quot;interest repaid derived&quot; for the first due repayment attached to the loan id</p>
  <p>if the amount repaid is less than or equal to the &quot;principal&quot; it update the &quot;principal repaid derived&quot; with the amount repaid and then update &quot;outstanding&quot; with (&quot;total due&quot; - &quot;principal repaid&quot;) for that first due payment</p>
  <p>if the amount repaid is less than or equal to the &quot;total due&quot; then it update &quot;principal repaid derived&quot; with the &quot;principal&quot; , update &quot;interest repaid derived&quot; with (&quot;total due&quot; - amount repaid) then update &quot;outstanding&quot; with (&quot;total due&quot; - &quot;principal repaid &quot; - &quot;interest repaid&quot;) for the first due repayment</p>
  <p>if the amount repaid is greater  than &quot;total due&quot; for that first repayment then update the &quot;principal repaid &quot; with principal , &quot;interest repaid&quot; with &quot;interest&quot; then go ahead to the next repayment with amount repaid as (&quot;amount repaid&quot; - &quot;total due &quot;) then loop over till last repayment</p>
  <p>my code output update all repayment associated with the id and thats not what i want
  if there is a better way to do this please guide me</p>
  <p>---------------------------my controller---------------------------</p>
  <pre><code>$loan = DB::table('tbl_loans')
                      -&gt;where('loan_id', $loan_id)
                      -&gt;get();
  
                  $repayment_schedules = DB::table('tbl_loan_repayment_schedule')
                      -&gt;where('loan_id', $loan_id)
                      -&gt;get();
  
                  $original_transactions = DB::table('tbl_loan_transactions')
                      -&gt;where('loan_id', $loan_id)
                      -&gt;whereIn('loan_transaction_type_id', [3, 8])
                      -&gt;orderBy('loan_transaction_id', 'asc')
                      -&gt;get();
  
                  $transactions = DB::table('tbl_loan_transactions')
                      -&gt;where('loan_id', $loan_id)
                      -&gt;whereIn('loan_transaction_type_id', [3, 8])
                      -&gt;orderBy('loan_transaction_id', 'asc')
                      -&gt;get();
  
                  foreach ($repayment_schedules as &amp;$repayment_schedule) {
  
                      $r_schedule = array();
  
                      $r_schedule['total_due'] = ($repayment_schedule-&gt;principal - $repayment_schedule-&gt;principal_repaid_derived)
                           +
                          ($repayment_schedule-&gt;interest - $repayment_schedule-&gt;interest_repaid_derived);
  
                      $r_schedule['principal_repaid_derived'] = 0;
                      $r_schedule['interest_repaid_derived'] = 0;
  
                      DB::table('tbl_loan_repayment_schedule')
                          -&gt;where('loan_id', $loan_id)
                          -&gt;orderBy('due_date', 'asc')
                          -&gt;update($r_schedule);
                  }
  
                  foreach ($transactions as &amp;$transaction) {
                      $amount = $transaction-&gt;amount;
                      $principal_repaid_derived = 0;
                      $interest_repaid_derived = 0;
  
                      //loop through repayment schedules
                      foreach ($repayment_schedules as &amp;$repayment_schedule) {
                          if ($amount &lt;= 0) {
                              break;
                          }
                          $principal = $repayment_schedule-&gt;principal - $repayment_schedule-&gt;principal_repaid_derived;
                          $interest = $repayment_schedule-&gt;interest - $repayment_schedule-&gt;interest_repaid_derived;
                          $due = $principal + $interest;
  
                          if ($due &lt;= 0) {
                              continue;
                          }
  
                          //allocate the payment
  
                          //principal
                          $schedule = array();
                          if ($amount &gt;= $principal &amp;&amp; $principal &gt; 0) {
                              $schedule['principal_repaid_derived'] = $repayment_schedule-&gt;principal_repaid_derived + $principal;
                              $principal_repaid_derived = $principal_repaid_derived + $principal;
                              $amount = $amount - $principal;
  
                          } elseif ($amount &lt; $principal &amp;&amp; $principal &gt; 0) {
                              $schedule['principal_repaid_derived'] = $repayment_schedule-&gt;principal_repaid_derived + $amount;
                              $principal_repaid_derived = $principal_repaid_derived + $amount;
                              $amount = 0;
                          }
                          //interest
                          if ($amount &gt;= $interest &amp;&amp; $interest &gt; 0) {
                              $schedule['interest_repaid_derived'] = $repayment_schedule-&gt;interest_repaid_derived + $interest;
                              $interest_repaid_derived = $interest_repaid_derived + $interest;
                              $amount = $amount - $interest;
                          } elseif ($amount &lt; $interest &amp;&amp; $interest &gt; 0) {
  
                              $schedule['interest_repaid_derived'] = $repayment_schedule-&gt;interest_repaid_derived + $amount;
                              $interest_repaid_derived = $interest_repaid_derived + $amount;
                              $amount = 0;
  
                          }
  
                          if (($repayment_schedule-&gt;principal - $repayment_schedule-&gt;principal_repaid_derived) + ($repayment_schedule-&gt;interest - $repayment_schedule-&gt;interest_repaid_derived) &lt;= 0) {
  
                              $schedule['paid_by_date'] = $transaction-&gt;submitted_on;
  
                          }
  
                          $schedule['total_due'] = ($repayment_schedule-&gt;principal - $repayment_schedule-&gt;principal_repaid_derived) + ($repayment_schedule-&gt;interest - $repayment_schedule-&gt;interest_repaid_derived);
  
                          DB::table('tbl_loan_repayment_schedule')
                              -&gt;where('loan_id', $loan_id)
                              -&gt;orderBy('due_date', 'asc')
                              -&gt;update($schedule);
  
                          // dd($schedule);
  
                          if ($amount &lt;= 0) {
                              break;
                          }
                      }
                      $tranx = array();
  
                      $tranx['principal_repaid_derived'] = $principal_repaid_derived;
                      $tranx['interest_repaid_derived'] = $interest_repaid_derived;
  
                      DB::table('tbl_loan_transactions')
                          -&gt;where('loan_id', $loan_id)
                          -&gt;whereIn('loan_transaction_type_id', [3, 8])
                          -&gt;orderBy('loan_transaction_id', 'asc')
                          -&gt;update($tranx);
                      if ($amount &lt;= 0) {
                          continue;
                      }
                  }
  
                  // echo json_encode($transactions);
                  $unchanged_transactions = [];
                  foreach ($original_transactions as $key) {
                      array_push($unchanged_transactions, [
                          $key-&gt;loan_transaction_id,
                          $key-&gt;loan_id,
                          $key-&gt;payment_type_id,
                          $key-&gt;amount,
                          $key-&gt;principal_repaid_derived,
                          $key-&gt;interest_repaid_derived,
                          $key-&gt;submitted_on,
                      ]);
                  }
                  $changed_transactions = [];
                  $count = 1;
                  foreach ($transactions as $key) {
                      array_push($changed_transactions, [
                          $key-&gt;loan_transaction_id,
                          $key-&gt;loan_id,
                          $key-&gt;payment_type_id,
                          $key-&gt;amount,
                          $key-&gt;principal_repaid_derived,
                          $key-&gt;interest_repaid_derived,
                          $key-&gt;submitted_on,
                      ]);
                      $count++;
                  }
  
                  function compare_multi_dimensional_array($array1, $array2)
                  {
                      $result = array();
                      foreach ($array1 as $key =&gt; $value) {
                          if (!is_array($array2) || !array_key_exists($key, $array2)) {
                              $result[$key] = $value;
                              continue;
                          }
                          if (is_array($value)) {
                              $recursiveArrayDiff = compare_multi_dimensional_array($value, $array2[$key]);
                              if (count($recursiveArrayDiff)) {
                                  $result[$key] = $recursiveArrayDiff;
                              }
                              continue;
                          }
                          if ($value != $array2[$key]) {
                              $result[$key] = $value;
                          }
                      }
                      return $result;
                  }
  
                  $transactions_to_be_updated = compare_multi_dimensional_array($changed_transactions, $unchanged_transactions);
  
                  foreach ($transactions_to_be_updated as $key =&gt; $value) {
                      $transaction = $unchanged_transactions[$key];
                      // dd($transactions_to_be_updated);
                  }
                  // dd($transactions_to_be_updated);
  </code></pre>