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] Support third-party relations in model:show command #51807

Merged
merged 1 commit into from
Jun 17, 2024

Conversation

staudenmeir
Copy link
Contributor

@staudenmeir staudenmeir commented Jun 14, 2024

The model:show command finds a model's relations by analyzing the code of its methods and looking for $this->hasMany( etc. This doesn't detect third-party relations (like eloquent-has-many-deep). We could build a whole system for packages to hook into, but that would be overkill, IMO.

Instead, we can check the method's return type and see if it's a subclass of the base Relation class. Not everybody uses return types, but I hope that most do nowadays.

I also added a check to skip methods with parameters. That's necessary to ignore some internal methods of third-party packages. Since relations shouldn't have parameters, I don't consider this a breaking change.

The command doesn't have any tests.

@calebdw
Copy link
Contributor

calebdw commented Jun 15, 2024

I also added a check to skip methods with parameters. That's necessary to ignore some internal methods of third-party packages.

I'm just curious, if you rejected all methods that weren't public would it solve this issue without having to specifically filter by the lack of parameters?

@staudenmeir
Copy link
Contributor Author

I'm just curious, if you rejected all methods that weren't public would it solve this issue without having to specifically filter by the lack of parameters?

@calebdw I would have preferred this approach, but the eloquent-has-many-deep package contains public methods that need to be ignored (but need to stay public).

@staudenmeir
Copy link
Contributor Author

staudenmeir commented Jun 15, 2024

Also: The command invokes all the methods it found and this fails if a method requires parameters.

$relation = $method->invoke($model);

@taylorotwell taylorotwell merged commit c2776c3 into laravel:11.x Jun 17, 2024
30 checks passed
@staudenmeir staudenmeir deleted the model-show-relations branch June 17, 2024 14:24
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