@@ -31,6 +31,15 @@ abstract class MorphOneOrMany extends HasOneOrMany
31
31
*/
32
32
protected $ morphClass ;
33
33
34
+ protected $ morphKeyTypeString ;
35
+
36
+ public function morphKeyTypeString ($ morphKeyTypeString = true )
37
+ {
38
+ $ this ->morphKeyTypeString = $ morphKeyTypeString ;
39
+
40
+ return $ this ;
41
+ }
42
+
34
43
/**
35
44
* Create a new morph one or many relationship instance.
36
45
*
@@ -63,16 +72,18 @@ public function addConstraints()
63
72
64
73
if (is_null (SchemaBuilder::$ defaultMorphKeyType )) {
65
74
parent ::addConstraints ();
66
- } else {
67
- $ query = $ this ->getRelationQuery ();
68
75
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 ;
75
77
}
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 );
76
87
}
77
88
}
78
89
@@ -198,7 +209,11 @@ protected function getPossibleInverseRelations(): array
198
209
#[\Override]
199
210
protected function getKeys (array $ models , $ key = null )
200
211
{
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
+ }
202
217
203
218
return (new Collection (parent ::getKeys ($ models , $ key )))
204
219
->transform (fn ($ key ) => $ castKeyToString === true ? (string ) $ key : $ key )
@@ -209,7 +224,7 @@ protected function getKeys(array $models, $key = null)
209
224
#[\Override]
210
225
protected function whereInMethod (Model $ model , $ key )
211
226
{
212
- if (! in_array (SchemaBuilder::$ defaultMorphKeyType , ['uuid ' , 'ulid ' , 'string ' ])) {
227
+ if (! in_array (SchemaBuilder::$ defaultMorphKeyType , ['uuid ' , 'ulid ' , 'string ' ]) && (! isset ( $ this -> morphKeyTypeString ) || $ this -> morphKeyTypeString === false ) ) {
213
228
return parent ::whereInMethod ($ model , $ key );
214
229
}
215
230
0 commit comments