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

[11.x] Add generic PHPDoc to relations #51681

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels;

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
* @template TChildModel of \Illuminate\Database\Eloquent\Model
*
* @extends Relation<TRelatedModel>
*/
class BelongsTo extends Relation
{
use ComparesRelatedModels,
Expand All @@ -19,7 +25,7 @@ class BelongsTo extends Relation
/**
* The child model instance of the relation.
*
* @var \Illuminate\Database\Eloquent\Model
* @var TChildModel
*/
protected $child;

Expand Down Expand Up @@ -195,8 +201,8 @@ public function match(array $models, Collection $results, $relation)
/**
* Associate the model instance to the given parent.
*
* @param \Illuminate\Database\Eloquent\Model|int|string|null $model
* @return \Illuminate\Database\Eloquent\Model
* @param TRelatedModel|int|string|null $model
* @return TChildModel
*/
public function associate($model)
{
Expand All @@ -216,7 +222,7 @@ public function associate($model)
/**
* Dissociate previously associated model from the given parent.
*
* @return \Illuminate\Database\Eloquent\Model
* @return TChildModel
*/
public function dissociate()
{
Expand All @@ -228,7 +234,7 @@ public function dissociate()
/**
* Alias of "dissociate" method.
*
* @return \Illuminate\Database\Eloquent\Model
* @return TChildModel
*/
public function disassociate()
{
Expand Down Expand Up @@ -290,7 +296,7 @@ protected function relationHasIncrementingId()
* Make a new related instance for the given model.
*
* @param \Illuminate\Database\Eloquent\Model $parent
* @return \Illuminate\Database\Eloquent\Model
* @return TRelatedModel
*/
protected function newRelatedInstanceFor(Model $parent)
{
Expand All @@ -300,7 +306,7 @@ protected function newRelatedInstanceFor(Model $parent)
/**
* Get the child of the relationship.
*
* @return \Illuminate\Database\Eloquent\Model
* @return TChildModel
*/
public function getChild()
{
Expand Down
45 changes: 25 additions & 20 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
use Illuminate\Support\Str;
use InvalidArgumentException;

/**
* @template T of \Illuminate\Database\Eloquent\Model
*
* @extends Relation<T>
*/
class BelongsToMany extends Relation
{
use InteractsWithDictionary, InteractsWithPivotTable;
Expand Down Expand Up @@ -583,7 +588,7 @@ public function orderByPivot($column, $direction = 'asc')
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model
* @return \Illuminate\Support\Collection<mixed, T>|T
*/
public function findOrNew($id, $columns = ['*'])
{
Expand All @@ -599,7 +604,7 @@ public function findOrNew($id, $columns = ['*'])
*
* @param array $attributes
* @param array $values
* @return \Illuminate\Database\Eloquent\Model
* @return T
*/
public function firstOrNew(array $attributes = [], array $values = [])
{
Expand All @@ -617,7 +622,7 @@ public function firstOrNew(array $attributes = [], array $values = [])
* @param array $values
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
* @return T
*/
public function firstOrCreate(array $attributes = [], array $values = [], array $joining = [], $touch = true)
{
Expand All @@ -643,7 +648,7 @@ public function firstOrCreate(array $attributes = [], array $values = [], array
* @param array $values
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
* @return T
*/
public function createOrFirst(array $attributes = [], array $values = [], array $joining = [], $touch = true)
{
Expand All @@ -669,7 +674,7 @@ public function createOrFirst(array $attributes = [], array $values = [], array
* @param array $values
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
* @return T
*/
public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true)
{
Expand All @@ -687,7 +692,7 @@ public function updateOrCreate(array $attributes, array $values = [], array $joi
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null
* @return T|\Illuminate\Database\Eloquent\Collection<mixed, T>|null
*/
public function find($id, $columns = ['*'])
{
Expand All @@ -705,7 +710,7 @@ public function find($id, $columns = ['*'])
*
* @param \Illuminate\Contracts\Support\Arrayable|array $ids
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection
* @return \Illuminate\Database\Eloquent\Collection<mixed, T>
*/
public function findMany($ids, $columns = ['*'])
{
Expand All @@ -725,7 +730,7 @@ public function findMany($ids, $columns = ['*'])
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection
* @return T|\Illuminate\Database\Eloquent\Collection<mixed, T>
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model>
*/
Expand All @@ -752,7 +757,7 @@ public function findOrFail($id, $columns = ['*'])
* @param mixed $id
* @param \Closure|array $columns
* @param \Closure|null $callback
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|mixed
* @return T|\Illuminate\Database\Eloquent\Collection<mixed, T>|mixed
*/
public function findOr($id, $columns = ['*'], ?Closure $callback = null)
{
Expand Down Expand Up @@ -784,7 +789,7 @@ public function findOr($id, $columns = ['*'], ?Closure $callback = null)
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return \Illuminate\Database\Eloquent\Model|static|null
* @return T|static|null
*/
public function firstWhere($column, $operator = null, $value = null, $boolean = 'and')
{
Expand All @@ -795,7 +800,7 @@ public function firstWhere($column, $operator = null, $value = null, $boolean =
* Execute the query and get the first result.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model|static|null
* @return T|static|null
*/
public function first($columns = ['*'])
{
Expand All @@ -808,9 +813,9 @@ public function first($columns = ['*'])
* Execute the query and get the first result or throw an exception.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model|static
* @return T|static
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model>
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<T>
*/
public function firstOrFail($columns = ['*'])
{
Expand All @@ -826,7 +831,7 @@ public function firstOrFail($columns = ['*'])
*
* @param \Closure|array $columns
* @param \Closure|null $callback
* @return \Illuminate\Database\Eloquent\Model|static|mixed
* @return T|static|mixed
*/
public function firstOr($columns = ['*'], ?Closure $callback = null)
{
Expand Down Expand Up @@ -859,7 +864,7 @@ public function getResults()
* Execute the query as a "select" statement.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection
* @return \Illuminate\Database\Eloquent\Collection<mixed, T>
*/
public function get($columns = ['*'])
{
Expand Down Expand Up @@ -1284,7 +1289,7 @@ public function allRelatedIds()
* @param \Illuminate\Database\Eloquent\Model $model
* @param array $pivotAttributes
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
* @return T
*/
public function save(Model $model, array $pivotAttributes = [], $touch = true)
{
Expand All @@ -1301,7 +1306,7 @@ public function save(Model $model, array $pivotAttributes = [], $touch = true)
* @param \Illuminate\Database\Eloquent\Model $model
* @param array $pivotAttributes
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
* @return T
*/
public function saveQuietly(Model $model, array $pivotAttributes = [], $touch = true)
{
Expand All @@ -1313,7 +1318,7 @@ public function saveQuietly(Model $model, array $pivotAttributes = [], $touch =
/**
* Save an array of new models and attach them to the parent model.
*
* @param \Illuminate\Support\Collection|array $models
* @param \Illuminate\Support\Collection<mixed, T>|array $models
* @param array $pivotAttributes
* @return array
*/
Expand All @@ -1331,7 +1336,7 @@ public function saveMany($models, array $pivotAttributes = [])
/**
* Save an array of new models without raising any events and attach them to the parent model.
*
* @param \Illuminate\Support\Collection|array $models
* @param \Illuminate\Support\Collection<mixed, T>|array $models
* @param array $pivotAttributes
* @return array
*/
Expand All @@ -1348,7 +1353,7 @@ public function saveManyQuietly($models, array $pivotAttributes = [])
* @param array $attributes
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
* @return T
*/
public function create(array $attributes = [], array $joining = [], $touch = true)
{
Expand Down
7 changes: 6 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

use Illuminate\Database\Eloquent\Collection;

/**
* @template T
*
* @extends HasOneOrMany<T>
*/
class HasMany extends HasOneOrMany
{
/**
* Convert the relationship to a "has one" relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
* @return \Illuminate\Database\Eloquent\Relations\HasOne<T>
*/
public function one()
{
Expand Down
Loading