Column Cannot Be Null Error in Laravel - Fix
This error occurs when trying to insert or update a NULL value into a column that doesn't allow NULL.
The Error
SQLSTATE[23000]: Integrity constraint violation: 1048 Column cannot be null
Common Causes
- 1 Required field not provided in request
- 2 Form field name doesn't match database column
- 3 Column not set as nullable in migration
- 4 Missing default value in migration
Solutions
Provide all required values
Post::create([
'title' => $request->title,
'body' => $request->body,
'user_id' => auth()->id(),
]);
Make column nullable in migration
Schema::table('posts', function (Blueprint $table) {
$table->string('subtitle')->nullable();
});
Add validation for required fields
$request->validate([
'title' => 'required|string|max:255',
'body' => 'required|string',
]);
Set default value in migration
$table->string('status')->default('draft');
Need Help With Your Laravel Project?
I specialize in building custom Laravel applications, process automation, and SaaS development. Whether you need to eliminate repetitive tasks or build something from scratch, let's discuss your project.
Currently available for 2-3 new projects
About Hafiz
Full Stack Developer from Italy. I build web applications with Laravel and Vue.js, and automate business processes. Creator of ReplyGenius, StudyLab, and other SaaS products.
View PortfolioRelated Errors
Duplicate Entry Error in Laravel - How to Fix
SQLSTATE[23000]: Integrity constraint vi...
ValidationException in Laravel - Handling Validation Errors
The given data was invalid - ValidationE...
SQLSTATE 42S02 Table Not Found in Laravel - Fix
SQLSTATE[42S02]: Base table or view not...