Invalid datetime format: 1366 Incorrect string value
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
In the development world, errors and exceptions are always lurking around. One such persistent issue faced by many developers is the "SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value..." error. This can especially be problematic when working with databases like MariaDB and MySQL, which follow the utf8mb4_unicode_ci character set. This blog will provide a comprehensive guide on how to solve this issue in Laravel apps that use these specific database settings.
Let's understand the error first. The error occurs when Laravel tries to add quotes around the column values containing special characters, non-ASCII characters, or Unicode symbols. However, doing so converts the Unicode characters into their hexadecimal representation using backslash (\x) notation. This results in an incorrect string value for the database column, triggering this error.
To fix this error, we can follow a few steps:
1. Use Laravel Eloquent's raw() method to skip any escaping or casting on your model attributes:Model::create([
'column-name' => \DB::raw('"Some text before 11 ▒ and other text after, and after."'),
...
]); Schema::create('mws_orders', function (Blueprint $table) {
...
$table->text('column-name')->nullable(); // change to an appropriate data type
...
});class Order extends Model {
protected $casts = [
'column-name' => function ($value) {
return preg_replace('/[^A-Za-z0-9\-\_\.\/:]/', '', $value); // strip non-ASCII and special characters from text
}
];
} $text = preg_replace('/[^A-Za-z0-9\-\_\.\/:]/', '', $text); // replace non-ASCII and special chars
Model::create([
'column-name' => $text,
...
]);