Skip to content

Commit

Permalink
Merge pull request #308 from lptn/fix-relation-return-type-after-orderBy
Browse files Browse the repository at this point in the history
Fix MorphMany return type when orderBy used
  • Loading branch information
lptn authored Jan 30, 2023
2 parents f9fa95c + ecc8f60 commit c723c0f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
20 changes: 20 additions & 0 deletions stubs/Relation.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ abstract class Relation
return $this->query->get($columns);
}

/**
* Add an "order by" clause for a timestamp to the query.
*
* Psalm note: ideally the type should be inferred from $this->query
*
* @param string|\Illuminate\Database\Query\Expression $column
* @return $this
*/
public function latest($column = null) {}

/**
* Add an "order by" clause for a timestamp to the query.
*
* Psalm note: ideally the type should be inferred from $this->query
*
* @param string|\Illuminate\Database\Query\Expression $column
* @return $this
*/
public function oldest($column = null) {}

/**
* Get the underlying query for the relation.
*
Expand Down
18 changes: 12 additions & 6 deletions tests/acceptance/EloquentRelationTypes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,22 @@ Feature: Eloquent Relation types
"""
final class Repository
{
/**
* @psalm-return MorphMany<Comment>
*/
/** @psalm-return MorphMany<Comment> */
public function getCommentsRelation(Video $video): MorphMany {
return $video->comments();
}
/**
* @psalm-return Collection<int, Comment>
*/
/** @psalm-return MorphMany<Comment> */
public function getLatestCommentsRelation(Video $video): MorphMany {
return $video->comments()->latest();
}
/** @psalm-return MorphMany<Comment> */
public function getOldestCommentsRelation(Video $video): MorphMany {
return $video->comments()->oldest();
}
/** @psalm-return Collection<int, Comment> */
public function getComments(Video $video): Collection {
return $video->comments;
}
Expand Down

0 comments on commit c723c0f

Please sign in to comment.