0
I’d like to know how to save values checkbox
within a while
, the field comprar01
the checkbox
who must return true
or false
.
I receive through the request()
:
$idproduto=$request->get('idproduto');
$qnt=$request->get('qnt');
$comprar01=$request->get('comprar01');
I do the while
in the controller
to save in the bank
$cont = 0;
while($cont < count($idproduto))
{
$detalhe = new MapaCompraDet();
$detalhe->idmapacompra = $dados->idmapacompra;
$detalhe->idproduto = $idproduto[$cont];
$detalhe->qnt = $qnt[$cont];
$detalhe->comprar01=$comprar01[$cont]; //aqui o problema
$detalhe->save();
$cont = $cont + 1;
Error:
I tried that
public function update(Request $request, $id){
$dados = MapaCompra::findOrFail($id);
$idproduto=$request->get('idproduto');
for ($i=0; $i < count($idproduto) ; $i++) {
$comprar01 = (isset($request['comprar01']))?$request['comprar01']:'inativo';
dd($comprar01);
}
He brings me in dd only those marked, when not mark it does not bring. I searched here and saw that I have to put indices in the checkbox before entering, I am trying. Behold: https://cursos.alura.com.br/forum/topico-inserindo-valores-de-checkbox-no-banco-de-dados-44489
CODE
<tbody id="tbodyCotacao">
@foreach($detalhes as $detalhe)
<tr>
<!-- ITENS COTAÇÃO 01 -->
<td>
<input style=" width: 60px" class="form-control" type="number" min="0" name="entrega01[]" value="{{$detalhe->entrega01}}">
</td>
<td>
<input style=" width: 70px" class="form-control" type="text" name="marca01[]" value="{{$detalhe->marca01}}">
</td>
<td>
<input id="pqnt" type="hidden" name="qnt[]" value="{{$detalhe->qnt}}">
<input id="vrunit01" style=" width: 65px;text-align:center" class="form-control" type="text" name="vrunit01[]" value="{{number_format($detalhe->vrunit01, 2, ',', '.') }}" onblur="multiplicar();" onblur="formatar();">
</td>
<td>
<input id="subtotal01" style="text-align:right;width: 75px" class="form-control subtotal01" type="hidden" name="subtotal01[]" readonly onblur="calcular();">
<input style="text-align:right;width: 75px" type="text" class="form-control subtotal01T" id="subtotal01T" value="{{number_format($detalhe->qnt*$detalhe->vrunit01, 2, ',', '.') }}" readonly>
<td style="text-align:center">
<input @if($detalhe->comprar01=='c') {!! 'checked="checked" ' !!}@endif type="checkbox" name="comprar01[]" value="c">
</td></tr>
It’s complicated to do that with
checkbox
, because PHP at the time of bringing information only brings what was selected, if it exists in the middlecheckbox
without selecting yourarray
$comprar01
is smaller than the others and will definitely give the question error. I would use one<select>
.– novic
True, he brought only the checked when not checked for error.
– frodrigues
How would it be with select? Give me an example please...
– frodrigues
In each line a combo with two options and value 0 and 1 with label no and yes
– novic
Entrendi, so it would be easy even, but my field this small to by a select, would have to be a checkbox even. Funny that in my registration worked normal, on this screen to edit that is giving this error.
– frodrigues
Possible duplicate of How to check if a checkbox is checked with PHP?
– novic
Take a look at this answer: https://answall.com/a/5716/54880 this is your solution, I even indicated your question as duplicate
– novic
Good morning Virgilio, I had already seen these topics, however I am testing but it is not working, it only takes the first items of the list. EX: If I have 3 lines, mark the 3 he picks the 1 understood. I’m trying to adjust but I still could not.
– frodrigues
You did what??? Post your html
– novic