Anna Jeanine
Anna Jeanine

Reputation: 4125

How to handle Form::checkbox in updating Eloquent?

I am struggling to update a certain model. The mode has the variable 'boolean_new_frame'. This value could either be 0 or 1. The configuration for the laravel form isn't working though. The code which I have now after doing some research keeps the value at 0...

Form:

<div class="form-group">
    {!! Form::label('boolean_new_frame', 'Pagina openen in nieuw venster?') !!}
    {!! Form::hidden('boolean_new_frame', false) !!}
    {!! Form::checkbox('boolean_new_frame', null, ['class' => 'form-control']) !!}
</div>

Controller:

public function update($id) {
  $rule  = Model::findOrFail($id);
  $input = \Input::except(['true_mail_attachment']);

  $rule->update($input);

  return \Redirect::route('admin.rules')
    ->with('message', 'Good!');
}

Could someone help me adjust the form/controller that the 'boolean_new_frame' variable is updated properly.

Upvotes: 1

Views: 232

Answers (1)

apokryfos
apokryfos

Reputation: 40653

You can concatenate with a default because when the checkbox is not checked there is no input entry for it:

Update (re-read the question):

$input["boolean_new_frame"] = !array_key_exists("boolean_new_frame",$input)?0:1;
$rule->update($input);

Upvotes: 1

Related Questions