Skip to content

Commit

Permalink
Add a second local key to HasManyThrough (#19114)
Browse files Browse the repository at this point in the history
  • Loading branch information
phroggyy authored and taylorotwell committed May 9, 2017
1 parent d3b4308 commit 757be73
Show file tree
Hide file tree
Showing 4 changed files with 438 additions and 329 deletions.
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

0 comments on commit 757be73

Please sign in to comment.