lev_
lev_

Reputation: 29

Update checkbox in Laravel 8

I have check box like this:

<div class="form-group clearfix">
  <div class="icheck-primary d-inline">
      <input type="hidden" name="type" value="0">
      <input type="checkbox" id="checkboxPrimary1" name="type" @if($role->type == 1) checked @endif>
      <label for="checkboxPrimary1">root</label>
  </div>
</div>

in controller:

$role = Role::find($id);
$role->type = $request->has('type');
$data = $request->all();
$role->update($data);

when the checkbox value is 1 then I can edit, but when the checkbox value is 0 then I get the error: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'type' at row 1 (SQL: update user_roles set type = 0, user_roles.updated_at = 2021-10-10 15:48:36 where id = 1)

the type of the field in the database table - enum('0','1')

Upvotes: 0

Views: 298

Answers (1)

Mainul Hasan
Mainul Hasan

Reputation: 699

This is happening because the value '0' datatype does not match the MySQL datatype.

You can alter your database column datatype from enum to varchar or string.

MYSQL: ALTER TABLE [user_roles] MODIFY COLUMN [type] VARCHAR(50)

Or Even you can try with laravel migration.

Upvotes: 1

Related Questions