Laravel 5.5 Collective checkbox

0

Then friends are fine with you, I'm starting in Laravel and I'm not able to solve a problem that should be simple.

I have a view that inserts data in db with a form built through collective and a single checkbox that saves 0 or 1 in the field.

So far so good, recording properly. But when I use the same form for editing the records, if I set any value in the checkbox at the time of updating the data it saves what is in the value '' defined in the collective in the case within the second parameter $pessoa->cliente , within my view I having a javascript function that when I mark or uncheck the checkbox it changes the value in the form. by the tests I've done here it has saved the information of the value that I set inside collective and as if it did not consider the value of the checkbox defined in javascript and only what I set to value in collective .

If I set the form as follows: {!! Form::label('cliente', 'Cliente') !!} {!! Form::checkbox('cliente', 'teste' , null, ['onclick' => 'myFunction()'']) !!}

it saves test even if it in the browser when I click on the checkbox it set another value for the same ... I apologize and hopefully it was clearer this time.

The Controller method follows.

public function atualizar($id, Request $request){
   $pessoa = Pessoa::findOrfail($id);
   $pessoa = $pessoa->update($request->all());
   return Redirect::to('pessoas/' . $id . '/editar');
   \Session::flash('mensagem_sucesso_atu', 'Atualizado com sucesso!');
}

Follow the excerpt from view :

{!! Form::label('cliente', 'Cliente') !!}
{!! Form::checkbox('cliente', $pessoa->cliente , null, ['onclick' => 'myFunction()','div' => 'cliente']) !!}
    
asked by anonymous 22.11.2017 / 23:24

1 answer

0
So for those who have the same problem at the time of updating the database in the case with forms that checkbox unchecked have to save some information the solution and very simple I lost hours with java script when the solution is inside the collective , it is only insert a hidden with the same name of the checkbox and put the value zero, as checkbox 'unchecked' brings a null you define that if it is unchecked its value is 0, that is exactly what I tried to do with javascript more for some reason did not record in the bank but follow how I solved ...

{{Form::hidden('cliente',0)}} {!! Form::checkbox('cliente') !!}

Of course not to find out by myself I found on this site another forum link .

    
24.11.2017 / 17:37