Laravel 5.5 Collective checkbox

Asked

Viewed 179 times

0

So friends all right with you, I’m starting in Laravel and I’m not getting to solve a problem that should be simple.

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

So far so good, recording correctly. However when I use the same form for editing the records, if I set any value in the checkbox when updating the data it saves what is in the value `` defined in collective in the case within the second parameter $pessoa->cliente, inside 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 that I have accomplished here it has saved the value information that I define within the collective as if it did not consider the value of the checkbox set in javascript and only what I set as value in collective.

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

it saves test even if there in the browser when I click on the checkbox it sets another value to the same ... I’m sorry but I hope I’ve been clearer this time.

Follows the method of Controller.

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!');
}

Follows the passage from view:

{!! Form::label('cliente', 'Cliente') !!}
{!! Form::checkbox('cliente', $pessoa->cliente , null, ['onclick' => 'myFunction()','div' => 'cliente']) !!}
  • Is your doubt confused? could improve

  • Let me try to summarize using form creator to create checkbox {!! Form::checkbox('client', '',null, ['onclick' => 'myFunction()','div' => 'client']) !! } it saves what it has set in the second parameter that in the example above this empty '', in my edit view I need to bring the value in the checkbox, something like this $person->id but when I call the method of updating the data it does not check what is set in my "html" I inspect the element .. it saves what is set in the value parameter then in the case of the above empty example ''

  • You have to put the controllers the model and your View in the question is to explain on top of that.

  • See if you’ve improved now

1 answer

0

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

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

Of course not discover alone I found on this site another forum https://laravel.io/forum/09-26-2014-handling-unchecked-checkbox-by-updating.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.