Skip to content

Commit ace4808

Browse files
committed
Refactor addConstraints method in Relations classes
1 parent 0b7b602 commit ace4808

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

src/Relations/BelongsToArrayColumn.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ public function __construct(Builder $query, Model $child, string $foreignKey, st
2424
*/
2525
public function addConstraints(): void
2626
{
27-
if (static::$constraints) {
28-
$query = $this->getBaseQuery();
27+
if (! static::$constraints) {
28+
return;
29+
}
30+
$query = $this->getBaseQuery();
2931

30-
$query->when($this->isString, function ($q) {
31-
$q->whereJsonContains($this->ownerKey, (string) $this->getParentKey());
32-
}, function ($q) {
33-
$q->whereJsonContains($this->ownerKey, $this->getParentKey());
34-
});
32+
$query->when($this->isString, function ($q) {
33+
$q->whereJsonContains($this->ownerKey, (string) $this->getParentKey());
34+
}, function ($q) {
35+
$q->whereJsonContains($this->ownerKey, $this->getParentKey());
36+
});
3537

36-
$query->whereNotNull($this->ownerKey);
37-
}
38+
$query->whereNotNull($this->ownerKey);
3839
}
3940

4041
/**

src/Relations/BelongsToManyKeys.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ public function __construct(Builder $query, Model $parent, protected string $for
4141
*/
4242
public function addConstraints(): void
4343
{
44-
if (static::$constraints) {
45-
$this->query->where(function ($query) {
46-
foreach ($this->localKeys as $localKey) {
47-
$query->orWhere(function ($query) use ($localKey) {
48-
$query->where($this->foreignKey, '=', $this->getParentKey($localKey))
49-
->whereNotNull($this->foreignKey);
50-
});
51-
}
52-
});
44+
if (! static::$constraints) {
45+
return;
5346
}
47+
$this->query->where(function ($query) {
48+
foreach ($this->localKeys as $localKey) {
49+
$query->orWhere(function ($query) use ($localKey) {
50+
$query->where($this->foreignKey, '=', $this->getParentKey($localKey))
51+
->whereNotNull($this->foreignKey);
52+
});
53+
}
54+
});
5455
}
5556

5657
/**

src/Relations/HasManyArrayColumn.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ class HasManyArrayColumn extends HasMany
1414
*/
1515
public function addConstraints(): void
1616
{
17-
if (static::$constraints) {
18-
$query = $this->getRelationQuery();
17+
if (! static::$constraints) {
18+
return;
19+
}
20+
$query = $this->getRelationQuery();
1921

20-
$query->wherein($this->foreignKey, $this->getParentKey());
22+
$query->wherein($this->foreignKey, $this->getParentKey());
2123

22-
$query->whereNotNull($this->foreignKey);
23-
}
24+
$query->whereNotNull($this->foreignKey);
2425
}
2526

2627
/**

src/Relations/HasManyKeys.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,19 @@ public function __construct(Builder $query, Model $parent, array $relations, pro
4242
*/
4343
public function addConstraints(): void
4444
{
45-
if (static::$constraints) {
46-
$foreignKeys = $this->foreignKeys;
47-
48-
$this->query->where(function ($query) use ($foreignKeys): void {
49-
foreach ($foreignKeys as $foreignKey) {
50-
$query->orWhere(function ($query) use ($foreignKey): void {
51-
$query->where($foreignKey, '=', $this->getParentKey())
52-
->whereNotNull($foreignKey);
53-
});
54-
}
55-
});
45+
if (! static::$constraints) {
46+
return;
5647
}
48+
$foreignKeys = $this->foreignKeys;
49+
50+
$this->query->where(function ($query) use ($foreignKeys): void {
51+
foreach ($foreignKeys as $foreignKey) {
52+
$query->orWhere(function ($query) use ($foreignKey): void {
53+
$query->where($foreignKey, '=', $this->getParentKey())
54+
->whereNotNull($foreignKey);
55+
});
56+
}
57+
});
5758
}
5859

5960
/**

0 commit comments

Comments
 (0)