Skip to content

Date always stored even if none changed #1024

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

Open
gldrenthe89 opened this issue May 15, 2025 · 10 comments
Open

Date always stored even if none changed #1024

gldrenthe89 opened this issue May 15, 2025 · 10 comments

Comments

@gldrenthe89
Copy link

PHP Version

8.3

Laravel Version

12.0

Package Version

Latest release

Description

A issue that has been reported before but no solution given: #483

I have the same issue where i have my database correctly set-up. The attribute is correctly being casted as 'datetime:Y-m-d' The save action has the same date as previous saved so the data after save was not dirty. But the audit is being logged as seen in below screenshot

Image

This in my opinion is a core issue. Audits should only be saved if a database record was indeed different than before. Or at least respect the casts instead of the 'carbon' value being recorded as new value, which isnt even possible to save in the database.

Please give this a proper look or give us the proper way to handle this so the database isnt being filled with unneeded data.

Steps To Reproduce

.

Possible Solutions

No response

@parallels999
Copy link
Contributor

Hi, could you upload a repo with just the source code for showing this problem? Or a failing test showing it?
It would be easier to solve it, it doesn't happen to me

@gldrenthe89
Copy link
Author

Hi i tested some more. I always use Carbon for dates and stuf because of the casts. But when i give the new date a Carbon formatted as Y-m-d. then it isnt triggered.
But then again back to my point. The audit check should only check wat was dirty after it has been saved. And even if it is done before then it should check if a casts exists and use that casts to format it., again check after save is in my opinion way better. The Spatie ActivityLog also can do this.

If i had the spare time to take a look at this i would have and i would have posted a PR.

@parallels999
Copy link
Contributor

@gldrenthe89
Copy link
Author

That is exactly what i am doing. Using atribute casting as shown in the given URL. It is casted as 'datetime:Y-m-d'

Image

@gldrenthe89
Copy link
Author

Maybe try getChanges()

Image

Image

But looks like this has only been added in the Laravel 12 documentation. But i can confirm in my oldest project this was already available in Laravel 7. I dont have projects using older versions.

The getDirty() does this:

Image

@parallels999
Copy link
Contributor

sounds feasible, try a PR

@gldrenthe89
Copy link
Author

unfortunaly i dont have the time for it, atm I have fixed it atm using mutators. I hope someone can take a look at this ;-)

@parallels999
Copy link
Contributor

#483 since 2019, I doubt it

@willpower232
Copy link
Contributor

I could only replicate this issue if I was applying the same value repeatedly in the same request, i.e. setting now() over and over again.

That said, Laravel probably shouldn't be identifying the value as dirty if it knows the effective part of the value hasn't actually changed. Both originalIsEquivalent and getChanges identify the value as changing even though it technically hasn't so without diving into the framework, I'm not sure how much we can do here.

Are you able to share whatever part of your code is triggering it to see if there is another way around it?

@gldrenthe89
Copy link
Author

Hi,

I am not able to share any code as it is proprietery. I have currently fixed it using a setter

public function startDate(): Attribute
    {
        return Attribute::make(
            set: static fn ($value) => isset($value) ? \Illuminate\Support\Carbon::parse($value)?->toDateString() : $value,
        );
    }

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

3 participants