Skip to content

Commit db7241b

Browse files
committed
Revert "Remove getHasCompareKey"
This reverts commit e8f390a.
1 parent e8f390a commit db7241b

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

src/Helpers/QueriesRelationships.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use function in_array;
2323
use function is_array;
2424
use function is_string;
25+
use function method_exists;
2526
use function strpos;
2627

2728
trait QueriesRelationships
@@ -113,14 +114,23 @@ public function addHybridHas(Relation $relation, $operator = '>=', $count = 1, $
113114
$not = ! $not;
114115
}
115116

116-
$key = $relation instanceof HasOneOrMany ? $relation->getForeignKeyName() : $relation->getOwnerKeyName();
117-
$relations = $hasQuery->pluck($key);
117+
$relations = $hasQuery->pluck($this->getHasCompareKey($relation));
118118

119119
$relatedIds = $this->getConstrainedRelatedIds($relations, $operator, $count);
120120

121121
return $this->whereIn($this->getRelatedConstraintKey($relation), $relatedIds, $boolean, $not);
122122
}
123123

124+
/** @return string */
125+
protected function getHasCompareKey(Relation $relation)
126+
{
127+
if (method_exists($relation, 'getHasCompareKey')) {
128+
return $relation->getHasCompareKey();
129+
}
130+
131+
return $relation instanceof HasOneOrMany ? $relation->getForeignKeyName() : $relation->getOwnerKeyName();
132+
}
133+
124134
/**
125135
* @param Collection $relations
126136
* @param string $operator

src/Relations/BelongsTo.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010
class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
1111
{
12+
/**
13+
* Get the key for comparing against the parent key in "has" query.
14+
*
15+
* @return string
16+
*/
17+
public function getHasCompareKey()
18+
{
19+
return $this->ownerKey;
20+
}
21+
1222
/** @inheritdoc */
1323
public function addConstraints()
1424
{

src/Relations/BelongsToMany.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121

2222
class BelongsToMany extends EloquentBelongsToMany
2323
{
24+
/**
25+
* Get the key for comparing against the parent key in "has" query.
26+
*
27+
* @return string
28+
*/
29+
public function getHasCompareKey()
30+
{
31+
return $this->getForeignKey();
32+
}
33+
2434
/** @inheritdoc */
2535
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
2636
{

src/Relations/HasMany.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@ public function getForeignKeyName()
2020
return $this->foreignKey;
2121
}
2222

23+
/**
24+
* Get the key for comparing against the parent key in "has" query.
25+
*
26+
* @return string
27+
*/
28+
public function getHasCompareKey()
29+
{
30+
return $this->getForeignKeyName();
31+
}
32+
2333
/** @inheritdoc */
2434
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
2535
{
26-
$foreignKey = $this->getForeignKeyName();
36+
$foreignKey = $this->getHasCompareKey();
2737

2838
return $query->select($foreignKey)->where($foreignKey, 'exists', true);
2939
}

src/Relations/HasOne.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ public function getForeignKeyName()
2020
return $this->foreignKey;
2121
}
2222

23+
/**
24+
* Get the key for comparing against the parent key in "has" query.
25+
*
26+
* @return string
27+
*/
28+
public function getHasCompareKey()
29+
{
30+
return $this->getForeignKeyName();
31+
}
32+
2333
/** @inheritdoc */
2434
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
2535
{

0 commit comments

Comments
 (0)