我正在构建一个应用程序,如果用户想要更新用户可以编辑和更新的任何记录,还要在前端添加更多字段,以便在字段中输入的数据可以添加到记录中,如果用户编辑任何内容,也可以成功更新更改。
因此,测试(问题)是用户应该做的,每个测试都有选项,就像检查一样。
当我尝试更改任何内容并单击更新按钮时,记录会成功更新,但如果我在表单中添加新输入,则只更新记录而不添加新记录。
这是我的HTML:
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Contraceptive pill</th>
<th>UID</th>
<th>Implant</th>
<th>Injectable</th>
<th>Male condom</th>
<th>Female condom</th>
<th>Fertility awareness</th>
<th>Withdrawal</th>
<th>Sterilization</th>
<th>Emergency contraceptive</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="test-inputs">
@foreach($Test0ptions as $option)
<tr>
<td></td>
<td>
<input class="form-control" type="number" name="contraceptive_pills[]" required value="{{ $option->contraceptive_pill }}">
</td>
<td>
<input class="form-control" type="number" name="uids[]" required value="{{ $option->uid}}">
</td>
<td>
<input class="form-control" type="number" name="implants[]" required value="{{ $option->implant}}">
</td>
<td>
<input class="form-control" type="number" name="injectables[]" required value="{{ $option->injectable}}">
</td >
<td>
<input class="form-control" type="number" name="male_condoms[]" required value="{{ $option->male_condom}}">
</td>
<td>
<input class="form-control" type="number" name="female_condoms[]" required value="{{ $option->female_condom}}">
</td>
<td>
<input class="form-control" type="number" name="fertility_awareness[]" required value="{{ $option->fertility_awareness}}">
</td>
<td>
<input class="form-control" type="number" name="withdrawals[]" required value="{{ $option->withdrawal}}">
</td>
<td>
<input class="form-control" type="number" name="sterilizations[]" required value="{{ $option->sterilization}}">
</td>
<td>
<input class="form-control" type="number" name="emergency_contraceptives[]" required value="{{ $option->emergency_contraceptive}}">
</td>
</tr>
</tbody>
</table>
<div class="form-group" style="padding-bottom: 10px;">
<button type="submit" class="btn primary"><i class="fa fa-save"></i> Update</button>
</div>
这是我的脚本,它将添加一组新的表单:
populateNewTestForms() {
$('#test-inputs').append(`
<tr>
<td>${this.incrementTableCounter+=1}</td>
<td><input class="form-control" type="number" name="contraceptive_pills[]" required> </td>
<td> <input class="form-control" type="number" name="uids[]" required> </td>
<td> <input class="form-control" type="number" name="implants[]" required> </td>
<td> <input class="form-control" type="number" name="injectables[]" required> </td>
<td> <input class="form-control" type="number" name="male_condoms[]" required> </td>
<td> <input class="form-control" type="number" name="female_condoms[]" required> </td>
<td> <input class="form-control" type="number" name="fertility_awareness[]" required> </td>
<td> <input class="form-control" type="number" name="withdrawals[]" required> </td>
<td> <input class="form-control" type="number" name="sterilizations[]" required> </td>
<td> <input class="form-control" type="number" name="emergency_contraceptives[]" required>
</td>
<td>
<button type="button" onclick=" $(this).closest('tr').remove(); " class="btn btn-danger" title="delete"><i class="fa fa-trash"></i></button>
</td>
</tr>
`);
这是我的控制者:
foreach ($request->option_ids as $option_id) {
TestOption::updateOrCreate(['id' => $option_id], [
'test_id' => $test->id,
'name' => $request->names[$i],
'contraceptive_pill' => $request->contraceptive_pills[$i],
'uid' => $request->uids[$i],
'implant' => $request->implants[$i],
'injectable' => $request->injectables[$i],
'male_condom' => $request->male_condoms[$i],
'female_condom' => $request->female_condoms[$i],
'fertility_awareness' => $request->fertility_awareness[$i],
'withdrawal' => $request->withdrawals[$i],
'sterilization' => $request->sterilizations[$i],
'emergency_contraceptive' => $request->emergency_contraceptives[$i]
]);
答案 0 :(得分:0)
您的功能仅搜索option
,请尝试将它们交换为此类。
TestOption::updateOrCreate([
'test_id' => $test->id
'name' => $request->names[$i],
'contraceptive_pill' => $request->contraceptive_pills[$i],
'uid' => $request->uids[$i],
'implant' => $request->implants[$i],
'injectable' => $request->injectables[$i],
'male_condom' => $request->male_condoms[$i],
'female_condom' => $request->female_condoms[$i],
'fertility_awareness' => $request->fertility_awareness[$i],
'withdrawal' => $request->withdrawals[$i],
'sterilization' => $request->sterilizations[$i],
'emergency_contraceptive' => $request->emergency_contraceptives[$i]
],['id' => $option_id]);