Database Laravel 8 Laravel 9 Laravel 10 Laravel 11 Laravel 12

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

Error Message
SQLSTATE[23000]: Integrity constraint violation: 1048 Column cannot be null

Common Causes

  1. 1 Required field not provided in request
  2. 2 Form field name doesn't match database column
  3. 3 Column not set as nullable in migration
  4. 4 Missing default value in migration

Solutions

1

Provide all required values

PHP
Post::create([
    'title' => $request->title,
    'body' => $request->body,
    'user_id' => auth()->id(),
]);
2

Make column nullable in migration

PHP
Schema::table('posts', function (Blueprint $table) {
    $table->string('subtitle')->nullable();
});
3

Add validation for required fields

PHP
$request->validate([
    'title' => 'required|string|max:255',
    'body' => 'required|string',
]);
4

Set default value in migration

PHP
$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

Hafiz Riaz

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 Portfolio

Related Errors