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

[11.x] Fallback to parent methods on HasUniqueStringIds trait #54096

Merged
merged 1 commit into from
Jan 6, 2025

Conversation

hafezdivandari
Copy link
Contributor

This PR makes HasUniqueStringIds trait respect $usesUniqueIds property and fallback to parent methods:

  • Determining if the model uses unique IDs before overriding uniqueIds() method and fallback to parent if not.
  • Fallback to parent method on getKeyType() and getIncrementing() method for consistency.

This is useful when a model is using this trait optionally by setting $usesUniqueIds property.

@taylorotwell taylorotwell merged commit 6755311 into laravel:11.x Jan 6, 2025
40 checks passed
@hafezdivandari hafezdivandari deleted the 11.x-unique-id-fallback branch January 6, 2025 20:10
@@ -38,7 +38,7 @@ public function initializeHasUniqueStringIds()
*/
public function uniqueIds()
{
return [$this->getKeyName()];
return $this->usesUniqueIds() ? [$this->getKeyName()] : parent::uniqueIds();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taylorotwell I don't really understand why, but this change specifically breaks PHPStan/Larastan.

It causes static analysis to think that every model ->id property is int.

I don't know why $this->usesUniqueIds() is false when running static analysis, though.

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