【问题标题】:Cannot update multiple rows with same value in Laravel Vue Axios无法在 Laravel Vue Axios 中更新具有相同值的多行
【发布时间】:2021-12-06 04:58:18
【问题描述】:

无法使用从 vue js 传递的单个值更新多行。我想使用数据库中的先前值向数据库列提前添加支付值。以及如何使用 axios 传递 PatientInfo.due-form.pay 的减法.

Vuejs 模板:

 <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>
     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 控制器:

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

【问题讨论】:

  • 好吧,您尝试在循环中访问$advance,但从不定义 $advance 是什么。您也可以在进行更新时分配它,然后再次保护它。只需执行 $p-&gt;advance += $request-&gt;pay; $p-&gt;save(); 也 - 如果您在 find 中省略 $id 周围的括号,find 只会返回一个条目,然后您也可以省略循环。
  • Hey Frnak,感谢您的回复。它显示“foreach() 参数必须是数组|对象类型,现在给定 null”这个错误。我认为它没有从前端获取数据。$advance 是我想用支付值更新的列名

标签: laravel vue.js axios


【解决方案1】:

如前所述,如果您只更新一个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']);
}

或者,如果您愿意,您也可以使用 update,但请注意,您的 advance 字段必须定义为可填充的。

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 已经保存您的更改。

在您的代码中 $advance 未定义。您无法像以前那样访问现有列。

【讨论】:

  • 感谢 Frnak 的详细回答。我尝试了你的两个代码,但它显示“在 null 上调用成员函数更新”所以我尝试了下面的代码并且它有效。$obj = DB:: table('患者') ->where('patient_id', '=', $request->id) ->increment('advance', $request->input('pay'));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
相关资源
最近更新 更多