How do I store 0 or 1 of a checkbox?

0

How do I store 0 or 1 of a checkbox with Laravel?

Html

<div class="custom-control custom-checkbox">
  <input type="checkbox" name="dashboard" value="1" class="custom-control-input" id="Dashboard">
  <label class="custom-control-label" for="Dashboard">Dashboard</label>
</div>
<div class="custom-control custom-checkbox">
  <input type="checkbox" name="cadastro_pessoas" class="custom-control-input" id="Cadastro">
  <label class="custom-control-label" for="Cadastro">Cadastro de Pessoas</label>
</div>  

Controller Method

public function store(Request $request)
    {   
        $usu = new Usuario();
        $usu->usr_login = $request->input('login');
        $usu->usr_senha = $request->input('senha');
        $usu->usr_nome = $request->input('nome');
        $usu->usr_email = $request->input('email');
        $usu->usr_acesso_dashboard = $request->input('dashboard');
        $usu->usr_acesso_cadastro_pessoas = $request->input('cadastro_pessoas');
        $usu->usr_acesso_cadasto_gerais = $request->input('gerais');
        $usu->usr_acesso_relatorios = $request->input('relatorio');
        $usu->usr_acesso_prontuario = $request->input('prontuario');
        $usu->usr_acesso_configuracoes = $request->input('configuracoes');
        $usu->usr_acesso_consultas = $request->input('consulta');
        $usu->usr_ativo = $request->input('ativo');
        $usu->usr_perfil = $request->input('perfil');
        $usu->usr_acesso_cadusuarios = $request->input('cadusuarios');
        $usu->save();
        return redirect('/usuario');
    }
    
asked by anonymous 14.11.2018 / 01:25

2 answers

0

The input checkbox when not selected does not pass value and how to solve this in Laravel?

In method $request->input pass the second parameter which is the default value, eg:

$request->input('dashbord', 0);

In other words, if it is chosen it will pass the value of it that is 1 if not the default value that is 0.

14.11.2018 / 02:35
0

When I work with checkbox I always leave a default on migrate :

$table->tinyInteger('dashboard')->default(0);

If the checkbox was not selected the request does not receive anything and to avoid doing other validations also use isset , because when checkbox has value it sends the value, but if it does not have 'on' send the following:

$usu->usr_acesso_dashboard = isset($request->dashboard)? 1 : 0;
    
21.11.2018 / 12:19