Home > Back-end >  Cannot update multiple rows with same value in Laravel Vue Axios
Cannot update multiple rows with same value in Laravel Vue Axios

Time:10-21

can't update multiple row with a single value passing from vue js.I want to add pay value with database column advance with previous value in database.And also how to pass substraction of patientInfo.due-form.pay using axios.

Vuejs template:

 <form @submit.prevent="updatePatientPayment">
 <table class="table">
 <tfoot>
    <tr>
        <td colspan="5" class="text-right">Total</td>
        <td  class="text-right">{{patientInfo.total}}</td>
    </tr>
     <tr>
        <td colspan="5" class="text-right">Advance Paid</td>
        <td  class="text-right">{{patientInfo.advance}}</td>
    </tr>
    <tr>
        <td colspan="5" class="text-right">Due</td>
        <td  class="text-right" >{{patientInfo.due}}</td>
    </tr>
    <tr>
        <td colspan="5" class="text-right">Payable</td>
       <input class="form-control text-right" type="number" v-model="form.pay"/>
    </tr>
    <tr>
        <td colspan="5" class="text-right">New Due</td>
        <td  class="text-right">{{patientInfo.due-form.pay}}</td>
    </tr>
</tfoot>
</table>
 <b-button type="submit" variant="success">Submit</b-button>
</form>

Vuejs Script:

    <script>
     export default {
     data(){
     return{
    id:this.$route.params.id,
   patient:[],
   patientInfo:{},
   form:{
    pay:0,
   }
    }
},

methods:{
    updatePatientPayment() {
      this.$http.post('http://127.0.0.1:8000/api/updatePatientPayment/'    this.id,this.form)
            .then(()=>{
                self.message = 'Data is entered';
                })   
    },
}
</script>

Laravel Controller:

 public function updatePatientPayment($id, Request $request)
 {
     $updatePatientPayment = Patient::find([$id]);
    foreach($updatePatientPayment as $p){
         $p->advance = $p->update([$advance   $request->pay]);
         $p->save();
     }
     return response()->json(['successfully updated']);
 }

CodePudding user response:

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.

  • Related