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

[5.6] Make it easier to extend relationship classes #22382

Closed
Closed
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
67 changes: 52 additions & 15 deletions src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports should be sorted by length. Helps review the PR.


trait HasRelationships
{
Expand Down Expand Up @@ -43,6 +43,23 @@ trait HasRelationships
'guessBelongsToManyRelation', 'findFirstMethodThatIsntRelation',
];

/**
* Define classes for relations
*
* @var array
*/
protected static $relationClasses = [
'hasOne' => HasOne::class,
'morphOne' => MorphOne::class,
'belongsTo' => BelongsTo::class,
'morphTo' => MorphTo::class,
'hasMany' => HasMany::class,
'hasManyThrough' => HasManyThrough::class,
'morphMany' => MorphMany::class,
'belongsToMany' => BelongsToMany::class,
'morphToMany' => MorphToMany::class,
];

/**
* Define a one-to-one relationship.
*
Expand All @@ -59,7 +76,9 @@ public function hasOne($related, $foreignKey = null, $localKey = null)

$localKey = $localKey ?: $this->getKeyName();

return new HasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
$relationClass = self::$relationClasses['hasOne'];

return new $relationClass($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
}

/**
Expand All @@ -82,7 +101,9 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey =

$localKey = $localKey ?: $this->getKeyName();

return new MorphOne($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);
$relationClass = self::$relationClasses['morphOne'];

return new $relationClass($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);
}

/**
Expand Down Expand Up @@ -117,7 +138,9 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat
// actually be responsible for retrieving and hydrating every relations.
$ownerKey = $ownerKey ?: $instance->getKeyName();

return new BelongsTo(
$relationClass = self::$relationClasses['belongsTo'];

return new $relationClass(
$instance->newQuery(), $this, $foreignKey, $ownerKey, $relation
);
}
Expand Down Expand Up @@ -161,7 +184,9 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
*/
protected function morphEagerTo($name, $type, $id, $ownerKey)
{
return new MorphTo(
$relationClass = self::$relationClasses['morphTo'];

return new $relationClass(
$this->newQuery()->setEagerLoads([]), $this, $id, $ownerKey, $type, $name
);
}
Expand All @@ -182,7 +207,9 @@ protected function morphInstanceTo($target, $name, $type, $id, $ownerKey)
static::getActualClassNameForMorph($target)
);

return new MorphTo(
$relationClass = self::$relationClasses['morphTo'];

return new $relationClass(
$instance->newQuery(), $this, $id, $ownerKey ?? $instance->getKeyName(), $type, $name
);
}
Expand Down Expand Up @@ -226,7 +253,9 @@ public function hasMany($related, $foreignKey = null, $localKey = null)

$localKey = $localKey ?: $this->getKeyName();

return new HasMany(
$relationClass = self::$relationClasses['hasMany'];

return new $relationClass(
$instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey
);
}
Expand Down Expand Up @@ -256,7 +285,9 @@ public function hasManyThrough($related, $through, $firstKey = null, $secondKey

$instance = $this->newRelatedInstance($related);

return new HasManyThrough($instance->newQuery(), $this, $through, $firstKey, $secondKey, $localKey, $secondLocalKey);
$relationClass = self::$relationClasses['hasManyThrough'];

return new $relationClass($instance->newQuery(), $this, $through, $firstKey, $secondKey, $localKey, $secondLocalKey);
}

/**
Expand All @@ -282,7 +313,9 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey =

$localKey = $localKey ?: $this->getKeyName();

return new MorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);
$relationClass = self::$relationClasses['morphMany'];

return new $relationClass($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);
}

/**
Expand Down Expand Up @@ -323,7 +356,9 @@ public function belongsToMany($related, $table = null, $foreignPivotKey = null,
$table = $this->joiningTable($related);
}

return new BelongsToMany(
$relationClass = self::$relationClasses['belongsToMany'];

return new $relationClass(
$instance->newQuery(), $this, $table, $foreignPivotKey,
$relatedPivotKey, $parentKey ?: $this->getKeyName(),
$relatedKey ?: $instance->getKeyName(), $relation
Expand Down Expand Up @@ -363,7 +398,9 @@ public function morphToMany($related, $name, $table = null, $foreignPivotKey = n
// appropriate query constraints then entirely manages the hydrations.
$table = $table ?: Str::plural($name);

return new MorphToMany(
$relationClass = self::$relationClasses['morphToMany'];

return new $relationClass(
$instance->newQuery(), $this, $name, $table,
$foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(),
$relatedKey ?: $instance->getKeyName(), $caller, $inverse
Expand Down