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

[9.x] Define a new method to create a through model #41444

Merged
merged 1 commit into from
Mar 14, 2022
Merged
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
15 changes: 13 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
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