Laravel insert fails with 'Field user_id doesn't have a default value'
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default valueShort 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.
Fix it fast
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());2Match what you see, then jump to its fix
- 1
The request has user_id, but getAttributes() does notThe column is not in $fillable, so create() throws it awayFix - 2
getAttributes() has no user_id even though it is fillableThe code never sets the valueFix - 3
Some records legitimately have no value for this columnThe column is optional but the table says NOT NULL with no defaultFix
- 1
What does the error look like?
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default valueThe 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.php2Fix
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
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
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 migrateConfirmed by laravel.com
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:
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
- EZ EstimaterConstruction quoting and job tracking for contractors, on a Laravel and Firebase stack.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
Related fixes
Databases
MySQL error 1364: Field 'id' doesn't have a default value on INSERT
Field '%s' doesn't have a default value (the server's message pattern; %s is the column name)
Error 1364 means the INSERT left out a NOT NULL column that has no default, and strict SQL mode (the default) refuses to guess.
3 causes, each with a fixBack end
Laravel migration cannot drop a column that has a foreign key
Error on rename of '%s' to '%s' (errno: %d - %s)
MySQL refuses to drop a column while a foreign key constraint still uses it.
2 causes, each with a fixBack end
Laravel: distinct() and count() give the total instead of the unique count
select('city')->distinct()->count() ignores the select
Laravel builds the count from the column you pass to count(), not from select().
2 causes, each with a fixBack end
Laravel Eloquent query for a column that is X or NULL
where('column', null) returns no rows
Wrap the pair in a closure: where(function ($q) { $q->where('col', $x)->orWhereNull('col'); }).
3 causes, each with a fixBack end
Laravel insert fails with Invalid datetime format 1292 Incorrect datetime value
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value
MySQL ran in strict mode and refused a date or time value that it cannot store.
3 causes, each with a fixBack 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