我正在使用Laravel 5.7.*
。
我有一个form
,它是formItems
,就像form HasMany formItems
和formItems belongsTo form
一样,当我点击“提交”时,我在DB
中只看到一个{{1 }}已存储,尽管我尝试存储多个formItems
。
这是我的formitem
:
store method
这是我的public function store(Request $request) {
$formItems = collect();
// dd($request);
// dd($request->formItems);
// // dd($familymember);
foreach($request['formItems'] as $formItem) {
$formItems->push(new FormItem([
'family_name' => $formItem['family_name'],
'family_phone' => $formItem['family_phone'],
]));
}
$form = Form::create([
'user_phone' => $request['user_phone'],
'user_cnic' => $request['user_cnic'],
]);
$form->formItems()->saveMany($formItems);
}
的HTML:
formItems
<form>
<fieldset>
<table class="table table-bordered fieldGroup">
<tbody>
<tr>
<td><input type="text" name="formItems[0][family_name]" class="form-control form-control-sm" placeholder="Full Name"></td>
<td><input type="text" name="formItems[0][family_phone]" class="form-control form-control-sm" placeholder="Phone"></td>
<td colspan="3">
<a href="javascript:void(0)" class="btn btn-success addMore"><span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span>+</a>
</td>
</tr>
</tbody>
</table>
<div class="form-group">
<div class="text-center">
<button class="btn btn-success" type="submit">Submit</button>
</div>
</div>
</fieldset>
</form>
<fieldset form="formId">
<table class="table table-bordered fieldGroupCopy" style="display: none;">
<tbody>
<tr>
<td><input type="text" name="formItems[0][family_name]" class="form-control form-control-sm" placeholder="Full Name"></td>
<td><input type="text" name="formItems[0][family_phone]" class="form-control form-control-sm" placeholder="Phone"></td>
<td><a style="align-items: center; justify-content: center; display: flex;" href="javascript:void(0)" class="btn btn-danger remove"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span>x</a></td>
</tr>
</tbody>
</table>
</fieldset>
型号:
From
class Form extends Model
{
protected $fillable = [
'user_phone',
'user_cnic',
];
protected $with = [
'formItems',
];
public function formItems()
{
return $this->hasMany(FormItem::class);
}
}
型号:
FromItem
class FormItem extends Model
{
protected $fillable = [
'form_id',
'family_name',
'family_phone',
];
public function form()
{
return $this->belongsTo(Form::class);
}
}
结果:
dd($request->all())
答案 0 :(得分:0)
请尝试以下代码
public function store(Request $request) {
$form = Form::create([
'user_phone' => $request['user_phone'],
'user_cnic' => $request['user_cnic'],
]);
$form_items = [];
foreach($request['formItems'] as $form_item) {
$form_items[] = new FormItem([
'form_id' => $form->id,
'family_name' => $form_item['family_name'],
'family_phone' => $form_item['family_phone']
]);
}
$form->formItems()->saveMany($form_items);
}
答案 1 :(得分:0)
我认为您重复了输入名称:
name="formItems[0][family_name]"
name="formItems[0][family_phone]"
您总是有这个名字,我想它应该是每个新项目的编号。例如:
name="formItems[0][family_name]"
name="formItems[0][family_phone]"
name="formItems[1][family_name]"
name="formItems[1][family_phone]"