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.4] add a "second local key" to HasManyThrough #18997

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
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
* @param string|null $firstKey
* @param string|null $secondKey
* @param string|null $localKey
* @param string|null $secondLocalKey
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function hasManyThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null)
public function hasManyThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null)
{
$through = new $through;

Expand All @@ -248,9 +249,11 @@ public function hasManyThrough($related, $through, $firstKey = null, $secondKey

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

$secondLocalKey = $secondLocalKey ?: $through->getKeyName();

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

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

/**
Expand Down
21 changes: 20 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class HasManyThrough extends Relation
*/
protected $localKey;

/**
* The local key on the intermediary model.
*
* @var string
*/
protected $secondLocalKey;

/**
* Create a new has many through relationship instance.
*
Expand All @@ -54,15 +61,17 @@ class HasManyThrough extends Relation
* @param string $firstKey
* @param string $secondKey
* @param string $localKey
* @param string $secondLocalKey
* @return void
*/
public function __construct(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey)
public function __construct(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
{
$this->localKey = $localKey;
$this->firstKey = $firstKey;
$this->secondKey = $secondKey;
$this->farParent = $farParent;
$this->throughParent = $throughParent;
$this->secondLocalKey = $secondLocalKey;

parent::__construct($query, $throughParent);
}
Expand Down Expand Up @@ -102,6 +111,16 @@ protected function performJoin(Builder $query = null)
}
}

/**
* Get the fully qualified parent key name.
*
* @return string
*/
public function getQualifiedParentKeyName()
{
return $this->parent->getTable().'.'.$this->secondLocalKey;
}

/**
* Determine whether "through" parent of the relation uses Soft Deletes.
*
Expand Down
Loading