Invalid datetime format: 1366 Incorrect string value
When you encounter SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value, it is often a red herring. The error message suggests that the database is trying to interpret a character sequence as a date or time, yet your schema clearly defines the column as text. In the specific case of the error message showing an invalid byte like \xBD followed by text content, the root cause is rarely a mismatch in column types or quoting logic. The issue almost always stems from how the data was inserted into the database in the first place.
The error message indicates that MariaDB encountered a character it could not store in the current character set. Your schema specifies utf8mb4_unicode_ci, which supports the full range of Unicode characters required for modern applications. However, the presence of \xBD suggests that binary data or an invalid UTF-8 sequence was written directly to the database without proper encoding handling. This usually happens during bulk imports (such as using CSV files with LOAD DATA INFILE) or when raw SQL queries are executed outside of Laravel's query builder, bypassing the automatic type casting and quoting mechanisms you expect.
Regarding your assumption that Laravel adds quotes to string values automatically: this is true for standard Eloquent queries generated via the ORM. When you use $model->save() or $model->update(), Laravel ensures the value is a valid string before binding it to the query, and the underlying PDO driver handles the quoting correctly. However, if you are populating the table using DB::insert with an array of raw values, or if you are importing data from a file where the byte order mark (BOM) or specific character encoding is mismatched between the source file and the database connection, Laravel will pass those bytes directly to the driver.
To resolve this, you must ensure that the data being introduced into the database matches the utf8mb4 encoding expectations. If you are importing from a CSV, verify that the file is saved as UTF-8 without BOM. You can check this using a hex editor or by running the following command in your shell:
file -i filename.csv
If the output indicates charset=binary or contains a byte order mark, you will need to convert it to standard UTF-8 before importing. You can use tools like iconv to force conversion if necessary:
iconv -f WINDOWS-1252 -t UTF-8 input.csv > output_utf8.csv
Another common pitfall involves the database connection itself. Even if your schema is set to utf8mb4, the server variable character_set_client and character_set_results must also be configured correctly to ensure data is sent and received with the proper encoding. You can verify this by querying the information schema:
SELECT @@character_set_client, @@character_set_connection, @@character_set_results;
If these values do not match utf8mb4, you may need to update your Laravel .env file or application configuration to explicitly set DB_CHARSET=utf8mb4. This ensures that every connection establishes the correct character set context immediately upon handshake.
Finally, inspect the specific row causing the error. If the \xBD byte is present in the source data but not intended, it indicates corruption at the ingestion point. Since your column is defined as text, Laravel will not truncate or sanitize binary garbage automatically; it expects clean, encoded strings from your application layer. Ensuring that all data pipelinesâwhether they involve form submissions, file uploads, or external integrationsâare explicitly encoding their output as UTF-8 before hitting the database will prevent this specific "Incorrect string value" error from recurring.