Checkbox value is null, Column 'situation' cannot be null

Asked

Viewed 86 times

1

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

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

1048 Column 'situation' cannot be null.

How can I solve?

The checkbox looks like this:

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

Already the catch is like this:

$civil = new civil;
$civil->situacao = $request->situacao;
$civil->save();
  • Gives this error only when the checkbox is not checked?

  • You can start with a preselected checkbox, and only allow changing the selection by not allowing everyone to be unchecked

  • Worked? .......

1 answer

0

The correct in this case would be with select and not with checkbox, because, force the choice of the text active or exonerated with the proposed structure, example:

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

I wouldn’t go through the same trouble, but I still want to keep checkbox would then have to check whether it was chosen or not, if chosen would be the value active if not exonerated:

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

Reference: Creating a Select Box Field

Browser other questions tagged

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