Laravel-admin: Empty fields cause mysql errors when saving (create or edit form)

Created on 2 Jul 2017  路  4Comments  路  Source: z-song/laravel-admin

When a field is empty or even with spaces, a mysql error is thrown, specifying that the field does not have a default value. If there is a character other than empty or spaces it works.

This seems like a call to empty() somewhere and causing the issue? ..the value should be empty string in the query string, but appears as null or undefined.

Most helpful comment

Ok, I've found the solution. There is not bug, it's a feature of Laravel, possibly to match with the default strict mode...

In this thread
https://laracasts.com/discuss/channels/laravel/laravel-54-auto-convert-empty-text-input-to-null

They suggest commenting the middleware \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class in app\Http\Kernel.php.

..it does work now as expected (in combination with the mysql strict mode false)

All 4 comments

When saving the edit form, a field with some characters that is empty will not cause an error, but will not save. However the other fields changes do save at the same time, as long as its not empty. No error is thrown during that operation...

For me it's a bug, unless i'm missing an important configuration detail ?
..and I don't want to "allow null" on my fields

I had problem with empty fields in my migrations. From this thread
https://laravel.io/forum/08-08-2015-eloquent-create-says-column-has-no-default-value

I've found that since Laravel 5.1, mysql connection is set to run in strict mode
In config/database.php I've changed strict to false for the mysql connection and it did work for my inserts.

However I still have a query exception when one field is left empty:

QueryException In Connection.php line 647 :
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null

Ok, I've found the solution. There is not bug, it's a feature of Laravel, possibly to match with the default strict mode...

In this thread
https://laracasts.com/discuss/channels/laravel/laravel-54-auto-convert-empty-text-input-to-null

They suggest commenting the middleware \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class in app\Http\Kernel.php.

..it does work now as expected (in combination with the mysql strict mode false)

For reference, to use strict mode and NULL values when input are empty, the fields allowed to be "empty" would need the "allow null" flag in the database.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

greentornado picture greentornado  路  3Comments

clock1129 picture clock1129  路  3Comments

MarKco picture MarKco  路  3Comments

amun1303 picture amun1303  路  3Comments

antranapp picture antranapp  路  3Comments