framework icon indicating copy to clipboard operation
framework copied to clipboard

[11.x] Add generic PHPDoc to relations

Open asanovr opened this issue 1 year ago • 5 comments

These changes add support to generic types for model relations.

Now, we can write types like this:

class User extends Model {
    /**
     * @return HasMany<Post>
     */
    public function posts(): HasMany {
        return $this->hasMany(Post::class);
    }
}

And we will get

$user->posts()->get() - Collection of Post $user->posts()->make() - Model of Post

asanovr avatar Jun 02 '24 05:06 asanovr

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

github-actions[bot] avatar Jun 02 '24 05:06 github-actions[bot]

@nunomaduro why didn't you do it earlier? 😄

siarheipashkevich avatar Jun 04 '24 13:06 siarheipashkevich

from a semantic perspective, don't "child" and "related" reference the same thing? Would "child" and "parent" be more appropriate?

browner12 avatar Jun 04 '24 15:06 browner12

We don't really have any extensive type test suit here so it's hard to ship this with confidence.

taylorotwell avatar Jun 05 '24 16:06 taylorotwell

Most of this is likely coming from the Larastan stubs (and I've thought of attempting a PR like this several times).

However, since we're looking to add this to the framework now I do want to call attention to @nagmat84's extensive PR to fix the Larastan relation generics: https://github.com/larastan/larastan/pull/1285. The PR was closed when @nunomaduro deleted the Larastan master branch and it just hasn't been reopened, but I believe it would be good to take the proposed changes under consideration.

calebdw avatar Jun 13 '24 05:06 calebdw

@asanovr, @driesvints, this can be closed now that #51851 has been merged

calebdw avatar Jul 10 '24 12:07 calebdw

Thanks @calebdw

driesvints avatar Jul 26 '24 08:07 driesvints