Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel insert fails with 'Field user_id doesn't have a default value'

ErrorSQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value

Short answer

Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it. Check whether the field is in $fillable (otherwise create() silently drops it), whether your code actually sets it, and whether the column should be nullable or have a default.

  • Applies to Laravel 12, PHP 8.3, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the attributes the model holds right before saving. If user_id is missing here, the value never got in.

    $post = new Post($request->validated());
    dd($post->getAttributes());
  2. 2Match what you see, then jump to its fix

    1. 1The request has user_id, but getAttributes() does notThe column is not in $fillable, so create() throws it awayFix
    2. 2getAttributes() has no user_id even though it is fillableThe code never sets the valueFix
    3. 3Some records legitimately have no value for this columnThe column is optional but the table says NOT NULL with no defaultFix

What does the error look like?

  • SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
  • The insert works when the column is removed or made nullable

Cause 1 of 3

The column is not in $fillable, so create() throws it away

1Check

Laravel protects models against mass assignment. Attributes that are not in $fillable are silently discarded by create() and fill().

grep -n -A3 'fillable' app/Models/Post.php

2Fix

Add the column to $fillable. During development, make Laravel throw instead of discarding quietly.

// app/Models/Post.php
protected $fillable = ['title', 'body', 'user_id'];

// AppServiceProvider::boot()
Model::preventSilentlyDiscardingAttributes(! app()->isProduction());

3Verify

Save again. The INSERT now carries user_id.

php artisan tinker --execute="dump(App\Models\Post::latest('id')->value('user_id'));"

Confirmed by laravel.com

Did this fix it?

Cause 2 of 3

The code never sets the value

1Check

A NOT NULL column with no DEFAULT has to be in every INSERT. MySQL treats a row without it as a missing value and, in strict mode (the default), aborts the statement.

php artisan tinker --execute="echo DB::selectOne('select @@sql_mode as m')->m;"

2Fix

Set the column in the controller before saving, for example from the signed-in user.

$post = Post::create([
    ...$request->validated(),
    'user_id' => $request->user()->id,
]);

3Verify

The save succeeds and the row shows the owner.

php artisan tinker --execute="dump(App\Models\Post::latest('id')->first()->toArray());"

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

The column is optional but the table says NOT NULL with no default

1Check

Look at the migration. A column with neither nullable() nor default() must always be given a value.

grep -rn "user_id" database/migrations/

2Fix

Add a migration that makes it nullable or gives it a default. When you change a column, repeat every modifier you want to keep, because missing ones are dropped.

Schema::table('posts', function (Blueprint $table) {
    $table->foreignId('user_id')->nullable()->change();
});

3Verify

Run the migration and repeat the failing insert.

php artisan migrate

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Set the owner column explicitly in one place, not in every controller.
  • Turn on preventSilentlyDiscardingAttributes outside production so missing fillable entries fail loudly.
  • Decide for each column whether it is required, nullable or defaulted when you write the migration.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

php artisan migrate stops with could not find driver

could not find driver (Connection: mysql, SQL: select * from information_schema.tables ...)

Laravel talks to the database through a PDO driver, and this error means the PHP that runs artisan has none for your DB_CONNECTION.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.