Latest

Monday, July 10, 2017

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

Asked by: dwdawdawdaw


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!


Answers

Answered by: Leo_Kelmendi at 2017-07-10 11:52PM Accepted



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';
}



Source

No comments:

Post a Comment

Adbox