Skip to content

Might not want to use $model->unset('attribute_or_model') #1184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MatthewDrouin opened this issue Apr 24, 2017 · 1 comment
Closed

Might not want to use $model->unset('attribute_or_model') #1184

MatthewDrouin opened this issue Apr 24, 2017 · 1 comment

Comments

@MatthewDrouin
Copy link

While testing we have determined that if you do

$model->attribute = 'blah';
$model->save();

$model->unset('attribute');
$model->isDity(); // will return false

however if you set it to null

$model->attribute = 'blah';
$model->save();

$model->attribute = null;
$model->isDity(); // will return true

The reason for this is that isDirty() calls getDirty() https://github.com/laravel/framework/blob/5.3/src/Illuminate/Database/Eloquent/Model.php#L3220 which loops over the current attributes. If the attribute is unset then it will not look at it.

So if you want to know if your object isDirty() you really should use null unless a trait or something is used with unset to mark it as dirty.

@GromNaN
Copy link
Member

GromNaN commented Aug 24, 2023

Fixed by #2578

@divine divine closed this as completed Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants