Skip to content
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

isDirty not working for json object #42788

Closed
vsg24 opened this issue Jun 13, 2022 · 3 comments
Closed

isDirty not working for json object #42788

vsg24 opened this issue Jun 13, 2022 · 3 comments

Comments

@vsg24
Copy link
Contributor

vsg24 commented Jun 13, 2022

  • Laravel Version: 8.83.16
  • PHP Version: 8.0
  • Database Driver & Version: mySQL 8.0

Description:

I'm trying to detect if an object is changed after the value of a property of a json field is updated. However, It's not detecting the change if the new value is a number with leading zeros. Note that the number is saved as a string so technically there shouldn't be any issues storing it along with all of its zeros.

Steps To Reproduce:

The aim is to update a property (account_number) of bban field (json object) on the $bankAccount object.
The code responsible for updating the values:

// dump the original value
dump($bankAccount->bban);
// update the json object
$bankAccount->bban = $this->dto->getBban();
// dump the updated value
dump($bankAccount->bban);
            
if ($bankAccount->isDirty(['bban'])) {
    // NEVER HITS!
    dd('values updated');
}

The result of dumping the json variable bban before and after the update:
Screenshot 2022-06-13 at 11 24 58

As can be seen, the value is updated however the isDirty method isn't able to detect the change, as a result, dd('values updated'); is never executed.

It only fails when the new value has leading zeros. Also worth noting is that I am casting the bban field to json as it is the recommended approach.

protected $casts = [
    'bban' => 'object',
];
@rodrigopedra
Copy link
Contributor

rodrigopedra commented Jun 13, 2022

This seems to be fixed by #41337

But that PR was only sent to 9.x. If maintainers consider this a bug fix, can you send a PR backporting it to 8.x?


EDIT, as a workaround, casting to json seems to work:

class BankAccount extends Model
{
    protected $casts = ['bban' => 'json'];
}

@vsg24
Copy link
Contributor Author

vsg24 commented Jun 13, 2022

@rodrigopedra Thank you, I do want to create a PR to fix it for 8.x as well.

@vsg24
Copy link
Contributor Author

vsg24 commented Jun 13, 2022

@rodrigopedra PR created. #42793

@vsg24 vsg24 closed this as completed Jun 13, 2022
RomainMazB added a commit to SunLabDev/wn-profile-plugin that referenced this issue Jul 18, 2022
Make eloquent isDirty/isClean method work. See laravel/framework#42788 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants