Skip to content

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

2 participants