I'm trying to safe some data in my database and get the following error:
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'nsfw' at row 1
The nsfw column has a 0 as a standart value. Thats my table:
The nsfw column is also in the models $fillable
array.
I want to detect if a checkbox is checked. If it is checked, nsfw should be 1. If it's not, nsfw should be 0.
Thats the checkbox HTML code:
<div class="form-group">
<label for="nsfw" class="control-label">NSFW</label>
<br>
<input type="checkbox" name="nsfw">
</div>
And thats the controller code:
if (Input::get('nsfw')) {
$nsfw = true;
} else {
$nsfw = false;
}
// dd($nsfw) gives me true or
//false back, depending if the checkbox is checked or not
$thread = new Thread();
$thread->name = Input::get('thread-name');
$thread->description = Input::get('thread-desc');
$thread->age_min = Input::get('thread-min-age');
$thread->age_max = Input::get('thread-max-age');
if ($nsfw == true) {
$thread->nsfw = 1;
} else {
$thread->nsfw = 0;
}
$thread->save();
Thanks for your help and sorry for my bad english!
Go to config/database.php
in the connections.mysql
set strict to false, then try to run it again, if you have no errors check if you have the data inserted correctly. If you don't then you are entering the wrong type of data to the mentioned column, so set your code to store values as strings:
if ($nsfw == true) {
$thread->nsfw = '1';
} else {
$thread->nsfw = '0';
}
When strict mode is true and you get errors, it means you have other errors in the code, for example a wrong datatype or a spelling error.
Leo's answer is right. I just want to emphasis, that you should reset the strict mode to true again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With