Unable to insert blank value in a field on which default ''
(blank) is set.
Run command to create a Laravel project:
composer create-project laravel/laravel Laravel-Err
Change value of DB_CONNECTION
from sqlite
to mysql
in .env
file:
DB_CONNECTION=mysql
Uncomment all DB_*
parameters in the .env
file and assign proper values.
Open \database\migrations\0001_01_01_000000_create_users_table.php
and make the following changes in it and save the file:
- Here is the main thing: Change the line from
$table->string('name');
to$table->string('name')->default('');
. - Remove these fields from the users's schema/table:
email
,email_verified_at
,password
,rememberToken
. - Remove whole schema/table
password_reset_tokens
.
Open \app\Models\User.php
and make the following changes and save the file.
- Remove
email
andpassword
from the$fillable
property. - Remove
$hidden
property. - Remove
casts
function.
In web.php
, I have set the following in post route:
Route::post('/', function (\Illuminate\Http\Request $request) {
// other code
})->withoutMiddleware(ConvertEmptyStringsToNull::class);
Notice the withoutMiddleware(ConvertEmptyStringsToNull::class)
, so that Laravel does not convert empty strings to null.
Open Terminal and run the command php artisan migrate
.
When the following is specified:
$validated = $request->validate([
'name' => 'max:255'
]);
The following error is occurring:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (Connection: mysql, SQL: `insert into users (name, updated_at, created_at) values (?, 2025-09-06 11:14:02, 2025-09-06 11:14:02)`)
This error is occurring also if I remove $request->validate([...]);
and specify User::create($request->all());
And when string
is specified in the validation rule:
$validated = $request->validate([
'name' => 'string|max:255'
]);
The following error is occurring:
The name field must be a string.
In both the cases, Laravel seems to be changing the value of the name
field to null
at different stages.
My question is what to do to insert blank ''
value into a NOT NULL
field?