Laravel 5 当记录不存在时,updateOrCreate实际上会更新,但不会创建
我正在构建一个应用程序,如果用户想要更新任何记录,用户可以编辑和更新,但也可以在前端添加更多字段,这样就可以将字段中输入的数据添加到记录中,如果用户编辑了任何内容,那么更新后的记录也可以成功更改 所以有一些测试(问题)是用户应该做的,每个测试都有选项,就像考试一样 当我尝试更改任何内容并单击“更新”按钮时,记录会成功更新,但如果我在表单中添加新输入,它只会更新记录,而不会添加新记录 这是我的HTML:Laravel 5 当记录不存在时,updateOrCreate实际上会更新,但不会创建,laravel-5,eloquent,Laravel 5,Eloquent,我正在构建一个应用程序,如果用户想要更新任何记录,用户可以编辑和更新,但也可以在前端添加更多字段,这样就可以将字段中输入的数据添加到记录中,如果用户编辑了任何内容,那么更新后的记录也可以成功更改 所以有一些测试(问题)是用户应该做的,每个测试都有选项,就像考试一样 当我尝试更改任何内容并单击“更新”按钮时,记录会成功更新,但如果我在表单中添加新输入,它只会更新记录,而不会添加新记录 这是我的HTML: <table class="table table-bordered"> &l
<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>
您的函数只搜索
选项
,尝试像这样交换它们
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]);
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]
]);
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]);