[Vuejs]-Cannot update multiple rows with same value in Laravel Vue Axios

0👍

As already mentioned you don’t need an array here if you only update one Patient.

public function updatePatientPayment($id, Request $request)
{
  // 1. Get the patient you want to update
  //    Do not wrap $id in [] if you only need to find one entity
  $patient = Patient::find($id);

  // 2. Change the value of the patient
  //    Do not use update when you assign the value to the model
  $patient->advance += $request->pay;

  // 3. Save the change
  $patient->save();

  // 4. Respond
  return response()->json(['successfully updated']);
}

Alternatively, you can also use update if you want to, but be aware that your advance field must be defined as fillable to do so.

public function updatePatientPayment($id, Request $request)
{
  // 1. Get the patient you want to update
  //    Do not wrap $id in [] if you only need to find one entity
  $patient = Patient::find($id);

  // 2. Update directly
  $patient->update(['advance' => $patient->advance + $request->pay]);

  // 3. Respond
  return response()->json(['successfully updated']);
}

update will already save your change.

In your code $advance is not defined. You cannot access the existing column as you did.

Leave a comment