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.

Hafiz Riaz

About Hafiz

Senior Full Stack Developer. I build production software with Laravel, Filament, Vue, and AI integrations, and write about the real decisions behind shipping it.

Get in touch →

Related Errors