1
I am trying to do a multiple Insert with Laravel 5.3
in a table as follows:
Form
{!! Form::open(['route' => 'demanda.store', 'class' => 'form']) !!}
<div class="form-group form-inline">
{!! Form::label('ano', 'Ano:'); !!}
{!! Form::text('ano', $ano, ['class'=>'form-control','readonly']); !!}
</div>
<div class="form-group form-inline">
@foreach ($unidades as $unidade)
<div class="form-group">
{!! Form::hidden('item_id[]', $items->id, null); !!}
{!! Form::hidden('ano[]', $ano); !!}
{!! Form::label('unidade', $unidade->sigla); !!}
{!! Form::hidden('unidade_id[]', $unidade->id, null); !!}
{!! Form::text('qtd[]', null,['class'=>'form-control','placeholder' =>'Demanda'])!!}
</div>
@endforeach
</div>
{!! Form::submit('Salvar', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
What would my controller look like to insert this data into my table, knowing that the fields in my table are:
ano
tem_id
unidade_id
qtd
Controller
public function store(DemandaFormRequest $request)
{
$dataForm = $request->all();
$insert = $this->demanda->insert($dataForm);
}
I get the following error:
QueryException in Connection.php line 770: Array to string conversion
It is because some fields of your form arrive as an array, you must do a foreach to insert each one
– Miguel