Value of checkbox is nil, Column 'situation' can not be null

1

I have a $table->enum('situacao', ['ativo', 'exonerado']); field.

I created a checkbox to do the registration, but it returns the following error:

  

1048 Column 'situation' can not be null.

How can I resolve?

The checkbox looks like this:

{!! Form::checkbox('situacao', '1') !!}

The catch is as follows:

$civil = new civil;
$civil->situacao = $request->situacao;
$civil->save();
    
asked by anonymous 22.12.2017 / 15:56

1 answer

0

The correct one would be with select and not with checkbox , because it would force the choice of active or exonerado text with the proposed structure,

{{ Form::select('situacao', ['ativo' => 'ativo','exonerado' => 'exonerado']) }}

I would not go through the same problem, but even if I wanted to continue with checkbox then I would have to check whether it was chosen or not, if it was chosen it would be the value active if not exonerado :

$civil = new civil;
$civil->situacao = $request->has('situacao') ? 'ativo' : 'exonerado';
$civil->save();

Reference: Creating a Select Box Field

    
22.12.2017 / 20:07