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

PersonalHomePage's avatar

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "user_id" violates not-null constraint

I'm trying to create an entry in another table when a new user registers. I put this in my RegisterController

protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'dob' => $data['dob'],
        ]);

        Season::create([
            'user_id', $user->id,
            'module', 0,
            'current', 0,
            'completed', 0,
        ]);

        return $user;
    }

Here's my table migration

    public function up()
    {
        Schema::create('seasons', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->foreignId('user_id')->constrained();
            $table->integer('module');
            $table->integer('current');
            $table->integer('completed');
        });
    }

I'm using postgres and for some reason it's telling me that my $user->id is passing null. What am I doing wrong here?

Here's the complete error message.

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "user_id" violates not-null constraint DETAIL: Failing row contains (1, 2020-09-22 18:44:21, 2020-09-22 18:44:21, null, null, null, null). (SQL: insert into "seasons" ("updated_at", "created_at") values (2020-09-22 18:44:21, 2020-09-22 18:44:21) returning "id") 
0 likes
7 replies
laracoft's avatar

Do a dd() and see what happens.

        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'dob' => $data['dob'],
        ]);
	dd($user);
PersonalHomePage's avatar

yes I used fillable.

protected $fillable = ['user_id', 'module' , 'current', 'completed'];
1 like
MarianoMoreyra's avatar

@alex_b that’s strange, because if you see the error it’s not just the id that’s passing as null but all the custom fields.

Could you post your Season model?

MichalOravec's avatar
Level 75

You have there

'user_id', $user->id,
'module', 0,
'current', 0,
'completed', 0,

But it should be an array, so instead of , should be =>

protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'dob' => $data['dob']
    ]);

    Season::create([
        'user_id' => $user->id,
        'module' => 0,
        'current' => 0,
        'completed' => 0
    ]);

    return $user;
}
1 like
isaac23's avatar

have just experienced the same error :

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "slug" of relation "products" violates not-null constraint...

but my solution : had forgotten to initiate slug on saving in my Modelcontroller $product ->slug = $request-> slug;

Please or to participate in this conversation.