Skip to content

[11.x] Make methods of HasRelationships generic#54174

Merged
taylorotwell merged 2 commits into
laravel:11.xfrom
SanderMuller:make-HasRelationships-methods-generic
Jan 13, 2025
Merged

[11.x] Make methods of HasRelationships generic#54174
taylorotwell merged 2 commits into
laravel:11.xfrom
SanderMuller:make-HasRelationships-methods-generic

Conversation

@SanderMuller

Copy link
Copy Markdown
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
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