In App\Http\Kernel.php, you find that a ConvertEmptyStringsToNull-middleware is referenced. If your empty string is sent in an HTTP-request, that's probably the source of your problem.
how to prevent eloquent from converting empty string to null automatically?
i'm trying to enter an empty string in a varchar column that is not nullable but laravel is converting that empty string to null when entering the record in the DB and hence i'm getting ** Integrity constraint violation: 1048 Column 'company' cannot be null** how to overcome this?
Do you fill the value from a form? Probably the ConvertEmptyStringsToNull middleware is your bottleneck. This is added in L5.4 see https://laravel-news.com/laravel-5-4-middleware
You can disable it in app/Http/Kernel.php it is listed at the top in the $middleware property.
This is done via middleware. Go to /app/Http/Kernel.php and comment this out of the protected $middleware array. Note this will affect all requests. It's either off or on.
// \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
Is the value sent by HTTP and if so, did you check that it arrives as an empty string and not as null?
I.e. is the problem really in the DB-insertion, or in the HTTP-transmission/reception?
What is received by the controller?
dd($request->fieldName);
What does your form field look like?
@Cronix the result of dd is null
@Krisell i'm not submitting a form, i'm importing data from an excel sheet. In the foreach loop this is some of the code that i'm using. the error occurs on the last line.
$mas = new Masterdealer();
$mas->macola_id = (int)$value->id;
$mas->created_at = $value->dateentered;
$mas->status = 1 - (int)$value->inactive;
$mas->fname = $value->first;
$mas->lname = $value->last;
$mas->company = $value->company == null ? " " : $value->company;
public function setCompanyAttribute($value = null)
{
$this->attributes['company'] = is_null($value) ? '' : $value;
}
However, this seems bad. If company field is not required, it's nullable... so let it be nullable. You can easily cast null to empty string, (string) null produces "", so you'd do (string) $value->company. Or add a company field to the casts property in your model.
@crnkovic it is required in the current system but right now i'm importing data from the excel sheet of the old system they didn't have the company field required, but now it's required.
Then just use the script thingy you did to import the data and you're done. Unless you're going to constantly import the sheet with no company column. Then you're better off allowing company field to be nullable in database, yet require it in validations.
i'm not submitting a form, i'm importing data from an excel sheet.
We all thought you were talking about submitting a form by your initial description. I'd add that middleware back, as that only has to do with input coming from a form request.
Personally I'd allow the field in the db to be null. That's really more appropriate, and takes up less storage space than an empty string.
@crnkovic the script thingy is not working, instead of entering this " " in the db it is entering null and hence the column can not be empty exception shows.
You can also always set a default value for that field in the db of an empty string. If null is inserted, it will be an empty string instead.
Schema::table('users', function (Blueprint $table) {
$table->string('company')->default('');
});
This shouldn't be marked as the "best answer", because it doesn't work (for most use-cases anyway): if you try to insert null into a non-nullable field, regardless of its default, the DBMS will complain. A default only works if you're not inserting anything for that field, by simply not mentioning it.
Anyway, I'm glad I found the ConvertEmptyStringsToNull reference from your first answer. :)
I came across this issue while I tried to implement an auto save function, because I had to add an entry to the database with an empty string for some columns.
For the Googles, the solution is as simple as:
public function setTitleAttribute($value)
{
$this->attributes['title'] = is_null($value) ? '' : $value;
}
Or in this case:
public function setCompanyAttribute($value)
{
$this->attributes['company'] = is_null($value) ? '' : $value;
}
I spent some time thinking about this today and came up with the following in case it helps anyone else who ends up here.
$model->attribute = $request->input('key', $model->attribute) ?? '';
Can someone point me to some educational information on why Laravel would force converting empty strings to NULL by default, please 😖🤔😢💩
@Snapey Thanks Snapey, but it doesn't educate on why they chose to convert empty strings to NULL by default. In my projects I get errors unless I remove that line.
What model & DB setup would have this as a positive effect, and why?
PS: I'm not going to set my strings as nullable, I want my DB to store string fields as strings, nothing else.
@Christofer Let's say a user can enter their phone number on a registration form. If they don't wish to disclose it and leave the field empty, that value is best described as null, not an empty string. That's exactly why DBs support nullable columns.
I had the same problem.
If you have the problem in a simple attribute, replace this with a Accesor & Mutators or Cast class in Laravel documentation. Its easy.
If the problem is the null value in a json. You can make convert to array with json_decode() and after that use the function array_walk_recursive(). Something like that:
$arr = json_decode( $json, true);
array_walk_recursive($arr,function (&$item, $key){
if($item == null){
$item = '';
}
});
Please or to participate in this conversation.