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] Make methods of HasRelationships generic #54174

Merged

Conversation

SanderMuller
Copy link
Contributor

This PR introduces generics to the relationResolver, newRelatedInstance, and newRelatedThroughInstance methods in the HasRelationships trait. By leveraging generics, these methods now provide stricter type checking for related models, improving developer experience and code predictability.

Key Changes:

  • Added generics to the relationResolver method, refining the return type to Closure|null.
  • Updated newRelatedInstance and newRelatedThroughInstance to ensure they return specific related model instances.
  • Enhanced method annotations with @template and @param to enforce stricter type handling.

Benefits:

  • Improved type safety for Eloquent relationships.
  • Enhanced IDE support and fewer runtime type errors.
  • Aligns with Laravel’s commitment to type clarity and maintainable code.

Before:

image

After:

image

This change is backward-compatible, maintaining existing functionality while improving the reliability and clarity of type handling in Eloquent relation methods.

@taylorotwell taylorotwell merged commit 7864240 into laravel:11.x Jan 13, 2025
40 checks passed
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.

2 participants