一次保存多条记录有问题。这是我的表格
{!! Form::open(array('url'=>'pipeline/save?return='.$return, 'class'=>'form-horizontal','files' => true , 'parsley-validate'=>'','novalidate'=>' ')) !!}
@foreach($destinazioni as $destinazione)
<div class="col-md-2 col-md-offset-2 text-left">
<input type="text" name="destination[]" readonly class="form-control" value="{{ $destinazione }}"/>
</div>
<div class="col-md-6 text-left">
<select name="target[]" class="form-control">
<option value="Nessuna">Seleziona...</option>
@foreach($campi->keys() as $target)
<option value="{{ $target }}"> {{ $target }} </option>
@endforeach
</select>
</div>
@endforeach
@if(!empty($profilo->set_attributi))
@foreach($profilo->attributi as $attributo)
<div class="col-md-2 col-md-offset-2 text-left">
<input type="text" name="destination[]" readonly class="form-control" value="{{ $attributo->nome_opzione }}"/>
</div>
<div class="col-md-6 text-left">
<select name="target[]" class="form-control">
<option value="Nessuna">Seleziona...</option>
@foreach($campi->keys() as $target)
<option value="{{ $target }}"> {{ $target }} </option>
@endforeach
</select>
</div>
@endforeach
@endif
<div style="clear:both"></div>
<div class="form-group" style="margin-top:5%">
<label class="col-sm-4 text-right"> </label>
<div class="col-sm-8">
<button type="submit" name="apply" class="btn btn-info btn-sm" ><i class="icon-checkmark-circle2"></i> {{ Lang::get('core.sb_apply') }}</button>
<button type="submit" name="submit" class="btn btn-primary btn-sm" ><i class="icon-bubble-check"></i> {{ Lang::get('core.sb_save') }}</button>
<button type="button" onclick="location.href='{{ URL::to('pipeline?return='.$return) }}' " class="btn btn-warning btn-sm "><i class="icon-cancel-circle2 "></i> {{ Lang::get('core.sb_cancel') }} </button>
</div>
</div>
{!! Form::close() !!}
这是我尝试处理数据插入的控制器:
$rules = $this->validateForm();
$validator = Validator::make($request->all(), $rules);
if ($validator->passes()) {
$request = array('destination' => $request->input('destination'), 'target' => $request->input('target'));
foreach ($request as $req) {
$destination = new Pipeline;
$destination->destination = $req->destination;
$destination->target = $req->target;
$destination->save();
}
return Redirect::to('pipeline')->with('messagetext',\Lang::get('core.note_success'))->with('msgstatus','success');
但实际上我会尝试获得非对象错误的属性。我也尝试过使用Pipeline :: insert($ data)方法,但它返回错误的列错误名称
答案 0 :(得分:2)
如果您知道如何构建数组,只需使用每个对象构建一个数组,如
$arrays = [
'destination' => $req->destination,
'target' => $req->target
]
然后一次性插入所有数组
DB::insert($arrayOfArrays);
如果您仍想按照自己的方式插入,请尝试
Pipeline::create([
...
]);
答案 1 :(得分:-1)
如果有人有这个问题我只是通过内爆解决并爆炸$ request,然后循环使用for cicle。很简单。
function postSave( Request $request)
{
$rules = $this->validateForm();
$validator = Validator::make($request->all(), $rules);
if ($validator->passes()) {
$idz = implode(",", $request->input('destination'));
$ids = explode(",", $idz);
$tgz = implode(",", $request->input('target'));
$tgs = explode(",", $tgz);
for ($i = 0; $i < count($ids); $i++)
{
$pipeline = new Pipeline;
$pipeline->destination = $ids[$i];
$pipeline->target = $tgs[$i];
$pipeline->save();
$i = $i++;
}