diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 089390c588b7..6cd9db19534a 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -115,7 +115,7 @@ protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localK */ public function hasOneThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null) { - $through = new $through; + $through = $this->newThroughInstance($through); $firstKey = $firstKey ?: $this->getForeignKey(); @@ -387,7 +387,7 @@ protected function newHasMany(Builder $query, Model $parent, $foreignKey, $local */ public function hasManyThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null) { - $through = new $through; + $through = $this->newThroughInstance($through); $firstKey = $firstKey ?: $this->getForeignKey(); @@ -759,6 +759,17 @@ protected function newRelatedInstance($class) }); } + /** + * Create a new model instance for a through model. + * + * @param string $class + * @return mixed + */ + protected function newThroughInstance($class) + { + return new $class; + } + /** * Get all the loaded relations for the instance. *