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

Model scope call after relation returns Builder #319

Merged
merged 2 commits into from
Oct 5, 2019
Merged

Model scope call after relation returns Builder #319

merged 2 commits into from
Oct 5, 2019

Conversation

canvural
Copy link
Collaborator

@canvural canvural commented Oct 5, 2019

Currently code like this $this->hasOne(User::class)->active(); results in error saying Call to an undefined method Illuminate\Database\Eloquent\Relations\HasOne::active(). This PR fixes this.

Relation class in Laravel forwards unknown calls to Builder. And Builder can call scope methods on the model. So it is safe to call model scopes from the relation, like above.

There was one issue that I couldn't solve. As far as my understanding goes for the Reflection and PHPStan, there is no way to know the calling context from the reflection.

So in this implementation, I'm treating every unknown call as a scope method call, if the method doesn't also exist in Builder

I'm open to suggestions if anyone knows how to get the actual scope method reflection from inside the ModelScopeAfterRelations pipe.

@nunomaduro nunomaduro merged commit 71cfed9 into larastan:master Oct 5, 2019
@nunomaduro
Copy link
Collaborator

Thanks!

@nunomaduro
Copy link
Collaborator

@canvural Let me know if you would like to maintain this project.

@canvural canvural deleted the fix-scope-access-on-relation branch October 5, 2019 20:31
@canvural
Copy link
Collaborator Author

canvural commented Oct 5, 2019

@nunomaduro I don't have too much experience with writing PHPStan extensions. I don't know if I'd be the right person.

I'm just fixing the errors that I found when running Larastan on my project. And I have some free time over the next 2 or 3 weeks. I'll continue to fix the issues I found. As for the maintaining let's see after this time.

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.

None yet

2 participants