Skip to content

Commit 7928ee3

Browse files
mathiasgrimmStyleCIBotcrynobone
authored
wip (#54728)
* wip * Apply fixes from StyleCI * wip * wip --------- Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent 210096b commit 7928ee3

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php

+25-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ abstract class MorphOneOrMany extends HasOneOrMany
3131
*/
3232
protected $morphClass;
3333

34+
protected $morphKeyTypeString;
35+
36+
public function morphKeyTypeString($morphKeyTypeString = true)
37+
{
38+
$this->morphKeyTypeString = $morphKeyTypeString;
39+
40+
return $this;
41+
}
42+
3443
/**
3544
* Create a new morph one or many relationship instance.
3645
*
@@ -63,16 +72,18 @@ public function addConstraints()
6372

6473
if (is_null(SchemaBuilder::$defaultMorphKeyType)) {
6574
parent::addConstraints();
66-
} else {
67-
$query = $this->getRelationQuery();
6875

69-
$query->where($this->foreignKey, '=', transform($this->getParentKey(), fn ($key) => match (SchemaBuilder::$defaultMorphKeyType) {
70-
'uuid', 'ulid', 'string' => (string) $key,
71-
default => $key,
72-
}));
73-
74-
$query->whereNotNull($this->foreignKey);
76+
return;
7577
}
78+
79+
$query = $this->getRelationQuery();
80+
81+
$query->where($this->foreignKey, '=', transform($this->getParentKey(), fn ($key) => match (SchemaBuilder::$defaultMorphKeyType) {
82+
'uuid', 'ulid', 'string' => (string) $key,
83+
default => $key,
84+
}));
85+
86+
$query->whereNotNull($this->foreignKey);
7687
}
7788
}
7889

@@ -198,7 +209,11 @@ protected function getPossibleInverseRelations(): array
198209
#[\Override]
199210
protected function getKeys(array $models, $key = null)
200211
{
201-
$castKeyToString = in_array(SchemaBuilder::$defaultMorphKeyType, ['uuid', 'ulid', 'string']);
212+
if (isset($this->morphKeyTypeString) && $this->morphKeyTypeString === true) {
213+
$castKeyToString = true;
214+
} else {
215+
$castKeyToString = in_array(SchemaBuilder::$defaultMorphKeyType, ['uuid', 'ulid', 'string']);
216+
}
202217

203218
return (new Collection(parent::getKeys($models, $key)))
204219
->transform(fn ($key) => $castKeyToString === true ? (string) $key : $key)
@@ -209,7 +224,7 @@ protected function getKeys(array $models, $key = null)
209224
#[\Override]
210225
protected function whereInMethod(Model $model, $key)
211226
{
212-
if (! in_array(SchemaBuilder::$defaultMorphKeyType, ['uuid', 'ulid', 'string'])) {
227+
if (! in_array(SchemaBuilder::$defaultMorphKeyType, ['uuid', 'ulid', 'string']) && (! isset($this->morphKeyTypeString) || $this->morphKeyTypeString === false)) {
213228
return parent::whereInMethod($model, $key);
214229
}
215230

0 commit comments

Comments
 (0)