Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.

sglara liked this thread

1

replied to similar post from the same era and author also:

we're having this issue.
on save(), laravel is changing our value of 0 to NULL. Any suggestions or ideas as to why and how we can stop that would be greatly appreciated.

Thanks.

Last updated 1 year ago.
0

Bumping this because it's killing us. We have a number of columns where the INT value is 0 and on save() laravel is changing them to NULL. Since our column in mysql is set to not null, it's kicking an error. As mentioned by the original post, the value is 0 when it hits save(). Something is happening in save() that is changing the value.

We've tried running the data through a little method to change NULL to DB::raw('DEFAULT'), and that works great for NULL values. The problem is, the value isn't NULL. It's 0. And we want it to be 0. save() is changing 0 to NULL.

Any thoughts, ideas, or suggestions are greatly appreciated.

Last updated 1 year ago.
0

I'm guessing this is only a Laravel 5 issue, because I find it hard to believe other people wouldn't have run into this a million times with 4. We're going back to 4. I'll let you know.

Last updated 1 year ago.
0

Do you have something like this in your models:

    static::saving(function($model){
        foreach ($model->toArray() as $key => $value) {
                $model->{$key} = empty($value) ? null : $value;
        }

        return true;
    });

???

Last updated 1 year ago.
0

Finally found the problem.

The problem occurs with float values of 0

so:

Model::find( 1 )->update( [ 'value' => floatval(0) ] ); // saves null
Model::find( 1 )->update( [ 'value' => intval(0) ] ); // saves 0

So the problem lies with the floats!

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.