Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - SQL - SQLSTATE[01000]: Warning: 1265 Data truncated for column 'nsfw' at row 1

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:

phpmyadminscreenshot

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!

like image 327
dwdawdawdaw Avatar asked Jul 10 '17 17:07

dwdawdawdaw


2 Answers

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';
}
like image 133
Leo Avatar answered Oct 18 '22 03:10

Leo


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.

like image 34
Alex Avatar answered Oct 18 '22 02:10

Alex