Skip to content

Commit

Permalink
define a new method to create a through model
Browse files Browse the repository at this point in the history
  • Loading branch information
hafezdivandari committed Mar 11, 2022
1 parent 6a05d15 commit 56831cb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 56831cb

Please sign in to comment.