Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Loomix's avatar

Updating CheckBox value to Database

Updating the record with the values changed in the form works fine but the checkbox value remains the same in database, weather it was checked or not. In database, I have a field multiassign which contains 0 or 1. I think there's something wrong with my html code.

html:

<div class="form-group{{ $errors->has('multiassign') ? '' : '' }}" style="float:left">
  <input type="checkbox" 
    class="form-control{{ $errors->has('multiassign') ? ' is-invalid' : '' }}" 
    name="multiassign" 
    value="{{ old('multiassign', $process->multiassign) }}" {{ $process->multiassign ? 'checked' : '' }} />
  @include('alerts.feedback', ['field' => 'multiassign'])
</div>

controller:

public function update(ProcessRequest $request, Process $process)
{
  Log::info($request->all());
  $process->update($request->all());
  return redirect()->route('process.index')->withStatus(__('Saved!'));
 }

log:

[2020-07-20 12:46:52] local.INFO: array (
   '_token' => 'yOQ7x5UfuWtj0MZjMAvIE5bqHuESZYR3geD0Bx0J',
   '_method' => 'put',
   'id' => 'X0013',
   'title' => 'Repair',
   'client' => 'Example Client',
   'multiassign' => '1',
)

in browser (for originally checked, value 1 in database)

<input type="checkbox" class="form-control" name="multiassign" value="1" checked />
0 likes
4 replies
MichalOravec's avatar
Level 75

If checkbox is not checked then his value doesn't send to the server, so you have to use has

$process->update($request->except('multiassign') + [
    'multiassign' => $request->has('multiassign')
]);

Docs: https://laravel.com/docs/7.x/requests#retrieving-input (look for Determining If An Input Value Is Present)

2 likes
tykus's avatar

Unchecked checkboxes do not appear in the Request (this is a legacy HTML thing); you need to set the value in the database based on the presence (or absence) of the checkbox form input:

public function update(ProcessRequest $request, Process $process)
{
  $process->update(array_merge($request->all(), ['multiassign' => $request->has('multiassign')]);
  return redirect()->route('process.index')->withStatus(__('Saved!'));
 }
Nakov's avatar

@loomix well, unfortunately if the checkbox is not checked you get nothing in the controller for that field, that's the reason why it won't turn to false at any point.

You might do this instead:

$process->update(
    $request->all() + ['multiassign' => $request->has('multiassign')]
);

Please or to participate in this conversation.