無法使用從 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']);
}
uj5u.com熱心網友回復:
如前所述,如果您只更新一個陣列,則此處不需要陣列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']);
}
或者,您也可以根據需要使用更新,但請注意,您的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中未定義。您無法像以前那樣訪問現有列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328317.html
上一篇:如何檢查購物車中每個產品的庫存
