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

shahr's avatar
Level 10

SQLSTATE[22007] Invalid datetime format 1366 Incorrect integer value:a field for column

After login a user, The user enters this skills form, selects the skills.

skills

*A user can enter multiple skills, and save in database. The field of name

  • skill_name

  • level And in table

  • skill_name

  • level

  • user_id I don't know if the skills and skill_user table should be, or not, I don't know. What would you do if you were me? Can you show me the cleanest coding?*

      public function up()
     {
      Schema::create('skills', function (Blueprint $table) {
          $table->bigIncrements('id');
          $table->bigInteger('user_id')->unsigned();
          $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
          $table->string('skill_name');
          $table->smallInteger('level');
          $table->timestamps();
      });
    
      Schema::create('skill_user', function (Blueprint $table) {
          $table->bigInteger('skill_id')->unsigned()->nullable();
          $table->foreign('skill_id')->references('id')->on('skills')->onDelete('cascade');
          $table->bigInteger('user_id')->unsigned()->nullable();
          $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
          $table->unique(['skill_id' , 'user_id']);
         });
        }
    

blade

showDynamicExperimental();
function showDynamicExperimental()
{
    let html = '' +
        '@foreach(auth()->user()->skills as $skill)\n'+
        '<div class="col-md-6 position-relative">\n' +
        '<i class="fas fa-times text-danger position-absolute"></i>' +
        '<div class="row">\n' +
        '<div class="col-md-8">\n' +
        '<div class="form-group">\n' +
        '<label for="skill_name">skill_name</label>\n' +
        '<input type="text" id="skill_name" name="skill_name[]" class="form-control" value="{{ $skill->skill_name }}">\n' +
        '</div>\n' +
        '</div>\n' +
        '<div class="col-md-4">\n' +
        '<div class="form-group">\n' +
        '<label for="level">level</label>\n' +
        '<select class="form-control" id="level" name="level[]">\n' +
        '<option value="1" {{ $skill->level == 1 ? 'selected' : '' }}>★☆☆☆☆</option>\n'+
        '<option value="2" {{ $skill->level == 2 ? 'selected' : '' }}>★★☆☆☆</option>\n'+
        '<option value="3" {{ $skill->level == 3 ? 'selected' : '' }}>★★★☆☆</option>\n'+
        '<option value="4" {{ $skill->level == 4 ? 'selected' : '' }}>★★★★☆</option>\n'+
        '<option value="5" {{ $skill->level == 5 ? 'selected' : '' }}>★★★★★</option>\n'+
        '</select>\n'+
        '</div>\n' +
        '</div>\n' +
        '</div>\n' +
        '</div>\n ' +
        '@endforeach';
    $('#showExperimental').append(html);
}

Controller

$user->skills()->detach();

if ($request->skill_name) {
    foreach ($request->skill_name as $key => $value) {
        $user->skills()->attach($value, [
            'user_id' => auth()->id(),
            'level' => $request->level[$key],
        ]);
    }
}

User.php

public function skills()   
{
    return $this->belongsToMany(Skill::class)->withPivot('skill_name');
}

I get this error.

error

0 likes
23 replies
sr57's avatar

Your title is about 'date format' and your error is 'missing level column'!? The error is due to the lack of level column definition in Schema::create('skill_user...

sr57's avatar

Change this Schema::create('skill_user... with the columns you want.

shahr's avatar
Level 10

@sr57

I did not understand what you meant.

What should I change?

Can you give an example?

sr57's avatar

Have you read the doc about column types?

sr57's avatar

What are you migrations?

shahr's avatar
Level 10

This

public function up()
{
    Schema::create('skills', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('skill_name');
        $table->smallInteger('level');
        $table->timestamps();
    });

    Schema::create('skill_user', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('skill_id')->unsigned()->nullable();
        $table->foreign('skill_id')->references('id')->on('skills')->onDelete('cascade');
        $table->bigInteger('user_id')->unsigned()->nullable();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->unique(['skill_id' , 'user_id']);
        $table->string('skill_name');
        $table->smallInteger('level');
    });
}
sr57's avatar

Yes, this is OK, you have a level column. But what is this code? Schema::create('skill_user', function (Blueprint $table) { $table->bigInteger('skill_id')->unsigned()->nullable(); Check directly in your database to see the structure of the table 'skill_user'

shahr's avatar
Level 10

This is the middle table for attaching.

shahr's avatar
Level 10

If you have a solution, put a sample full code here.

In fact, my code is all wrong. If you were me, How did you save the skills?

sr57's avatar

First answer my question.

shahr's avatar
Level 10

@sr57

But what is this code?

This code is for attaching of the database.

sr57's avatar

I don't think so. So I ask you to check directly in your database to see the structure of the table 'skill_user'

sr57's avatar

In your migration level is a small integer, and in your db, a string! It's probably not the cause of your error but that means that your app / db are not coherent. You have first to refresh your migration.

sr57's avatar

php artisan migrate:refresh

but in your case I should recreate the app from the very beginning.

shahr's avatar
Level 10

Is there anyone to answer my question?

sr57's avatar

Do you follow my recomandation?

shahr's avatar
Level 10

I changed this code

//dd($request->skill_name);
$user->skills()->detach();
if ($request->skill_name) {
    foreach ($request->skill_name as $key => $skill_name) {
        $user->skills()->sync($skill_name, [
            'user_id' => auth()->id(),
            'level' => $request->level[0],
            'skill_name' => $request->skill_name[0],
        ]);
    }
}

I get this error.

SQLSTATE[HY000]: General error: 1364 Field 'skill_name' doesn't have a default value (SQL: insert into skill_user (skill_id, user_id) values (Laravel, 1))

sr57's avatar

Please write in capital letters 1 plus the square root of 25

2 likes

Please or to participate in this conversation.