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

feat: adds new rule to check for relation existence #985

Merged
merged 1 commit into from
Oct 29, 2021

Conversation

canvural
Copy link
Collaborator

  • Added or updated tests
  • Documented user facing changes
  • Updated CHANGELOG.md

Resolves #974

This PR adds a new rule to check for the existence of the relations. It also supports nested relations.

@canvural canvural merged commit 03f1fb6 into master Oct 29, 2021
@canvural canvural deleted the relation-existence-rule branch October 29, 2021 10:02
@bernardwiesner
Copy link

@canvural thanks for the feature.

Is it necessary inside Models to specify the return type?

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

If I do not specify the return type I get an error such as:

Relation 'user' is not found in App\Post model.

I thought the relation is already returned in laravel HasRelationships class, but doesnt seem to work without specifying.

@canvural
Copy link
Collaborator Author

canvural commented Feb 9, 2022

@bernardwiesner Yes you need to have return types for your relations. Larastan needs to know if a relation method is in your model file. And it does that by checking the method name and return types. PHPStan does not scan method bodies, so we only see method names, comments and return types.

And no need to open a new issue.

@bernardwiesner
Copy link

@canvural I see, thanks for explaining!

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.

Check for relation existence in Builder methods
3 participants