-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[12.x] Add $key
parameter to getChanges()
and getPrevious()
methods
#56659
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
[12.x] Add $key
parameter to getChanges()
and getPrevious()
methods
#56659
Conversation
getChanges()
and getPrevious()
methods$key
parameter to getChanges()
and getPrevious()
methods
{ | ||
return $this->changes; | ||
return (new static)->setRawAttributes( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can avoid extra steps if key is null and just return early. Valid for getPrevious also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still to avoid the return type change, maybe new functions would better suit this case getChange, getPreviousByKey.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review!
The additions in this PR are nearly identical to the existing getOriginal
method, so I'll wait for the Laravel team's thoughts on this before changing 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Following old paths is not always the best thing to do because if those paths are wrong... Multi-return type is one of those wrong paths to follow. Also introducing more steps if $key is null is another bad path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistency is far more important for the longevity of codebase. Implementing new ways of doing things when an existing pattern already exists fragments codebases and causes developer turmoil.
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
Could this be a breaking change because we are transforming model values in those methods you're calling? |
@taylorotwell Yes thats correct! No problem though -- can maybe look at this again in the future 👍 |
This PR adds optional
$key
and$default
parameters to thegetChanges()
andgetPrevious()
methods in theHasAttributes
trait, allowing developers to retrieve specific changed or previous attribute values directly:Before:
After:
This aligns directly with the existing
getOriginal
method:framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Lines 1986 to 1998 in 6d26643
This may be considered to be a breaking change, since developers who have overridden
getPrevious()/getChanges()
would have to support these new parameters. Otherwise they will receive this error:I can target Laravel 13 if you prefer -- let me know! Thanks for your time!