Skip to content

[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

Closed

Conversation

stevebauman
Copy link
Contributor

@stevebauman stevebauman commented Aug 14, 2025

This PR adds optional $key and $default parameters to the getChanges() and getPrevious() methods in the HasAttributes trait, allowing developers to retrieve specific changed or previous attribute values directly:

Before:

$changedName = $user->getChanges()['name'] ?? null;
$previousTitle = $user->getPrevious()['title'] ?? 'default';

After:

$changedName = $user->getChanges('name');
$previousTitle = $user->getPrevious('title', 'default');

This aligns directly with the existing getOriginal method:

/**
* Get the model's original attribute values.
*
* @param string|null $key
* @param mixed $default
* @return ($key is null ? array<string, mixed> : mixed)
*/
public function getOriginal($key = null, $default = null)
{
return (new static)->setRawAttributes(
$this->original, $sync = true
)->getOriginalWithoutRewindingModel($key, $default);
}

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:

Fatal error: Declaration of ...getPrevious() must be compatible with Illuminate\Database\Eloquent\Model::getPrevious($key = null, $default = null)

I can target Laravel 13 if you prefer -- let me know! Thanks for your time!

@stevebauman stevebauman changed the title [12.x] Add key parameter to getChanges() and getPrevious() methods [12.x] Add $key parameter to getChanges() and getPrevious() methods Aug 14, 2025
{
return $this->changes;
return (new static)->setRawAttributes(

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.

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.

Copy link
Contributor Author

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 👍

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.

Copy link
Contributor Author

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.

@taylorotwell
Copy link
Member

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!

@taylorotwell
Copy link
Member

Could this be a breaking change because we are transforming model values in those methods you're calling?

@stevebauman
Copy link
Contributor Author

@taylorotwell Yes thats correct! No problem though -- can maybe look at this again in the future 👍

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

Successfully merging this pull request may close these issues.

3 participants