[11.x] Add generic PHPDoc to relations
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
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.
@nunomaduro why didn't you do it earlier? 😄
from a semantic perspective, don't "child" and "related" reference the same thing? Would "child" and "parent" be more appropriate?
We don't really have any extensive type test suit here so it's hard to ship this with confidence.
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.
@asanovr, @driesvints, this can be closed now that #51851 has been merged
Thanks @calebdw