15
15
use Illuminate \Database \Eloquent \Relations \MorphOne ;
16
16
use Illuminate \Database \Eloquent \Relations \MorphTo ;
17
17
use Illuminate \Database \Eloquent \Relations \MorphToMany ;
18
+ use Illuminate \Database \Eloquent \Relations \Relation ;
19
+ use Illuminate \Support \Str ;
18
20
use MacropaySolutions \LaravelCrudWizard \Models \BaseModel ;
19
21
20
22
/**
@@ -184,6 +186,37 @@ public function __construct(
184
186
185
187
return parent ::__construct ($ query , $ parent , $ foreignKey , $ localKey );
186
188
}
189
+
190
+ public function one ()
191
+ {
192
+ if (!\method_exists (HasMany::class, 'one ' )) {
193
+ return parent ::one (...func_get_args ());
194
+ }
195
+
196
+ $ relationName = Str::uuid ()->toString ();
197
+
198
+ return Relation::macroNoConstraints (function () use ($ relationName ): HasOne {
199
+ $ this ->parent ->nowEagerLoadingRelationNameWithNoConstraints = $ relationName ;
200
+
201
+ $ hasOne = new HasOne (
202
+ $ this ->getQuery (),
203
+ $ this ->parent ,
204
+ $ this ->foreignKey ,
205
+ $ this ->localKey
206
+ );
207
+
208
+ if (
209
+ \method_exists ($ this , 'getInverseRelationship ' )
210
+ && \method_exists (HasOne::class, 'inverse ' )
211
+ ) {
212
+ if ($ inverse = $ this ->getInverseRelationship ()) {
213
+ $ hasOne ->inverse ($ inverse );
214
+ }
215
+ }
216
+
217
+ return $ hasOne ;
218
+ }, $ relationName );
219
+ }
187
220
};
188
221
}
189
222
@@ -231,6 +264,31 @@ public function __construct(
231
264
$ secondLocalKey
232
265
);
233
266
}
267
+
268
+ public function one ()
269
+ {
270
+ if (!\method_exists (HasManyThrough::class, 'one ' )) {
271
+ return parent ::one (...func_get_args ());
272
+ }
273
+
274
+ $ relationName = Str::uuid ()->toString ();
275
+
276
+ return Relation::macroNoConstraints (function () use ($ relationName ): HasOneThrough {
277
+ $ this ->farParent ->nowEagerLoadingRelationNameWithNoConstraints = $ relationName ;
278
+ $ builder = $ this ->getQuery ()->clone ();
279
+ $ builder ->setQuery ($ builder ->getQuery ()->clone ());
280
+
281
+ return new HasOneThrough (
282
+ \tap ($ builder , fn (Builder $ query ): array => $ query ->getQuery ()->joins = []),
283
+ $ this ->farParent ,
284
+ $ this ->throughParent ,
285
+ $ this ->getFirstKeyName (),
286
+ $ this ->secondKey ,
287
+ $ this ->getLocalKeyName (),
288
+ $ this ->getSecondLocalKeyName (),
289
+ );
290
+ }, $ relationName );
291
+ }
234
292
};
235
293
}
236
294
@@ -252,6 +310,38 @@ public function __construct(
252
310
253
311
return parent ::__construct ($ query , $ parent , $ type , $ id , $ localKey );
254
312
}
313
+
314
+ public function one ()
315
+ {
316
+ if (!\method_exists (MorphMany::class, 'one ' )) {
317
+ return parent ::one (...func_get_args ());
318
+ }
319
+
320
+ $ relationName = Str::uuid ()->toString ();
321
+
322
+ return Relation::macroNoConstraints (function () use ($ relationName ): MorphOne {
323
+ $ this ->getParent ()->nowEagerLoadingRelationNameWithNoConstraints = $ relationName ;
324
+
325
+ $ morphOne = new MorphOne (
326
+ $ this ->getQuery (),
327
+ $ this ->getParent (),
328
+ $ this ->morphType ,
329
+ $ this ->foreignKey ,
330
+ $ this ->localKey ,
331
+ );
332
+
333
+ if (
334
+ \method_exists ($ this , 'getInverseRelationship ' )
335
+ && \method_exists (MorphOne::class, 'inverse ' )
336
+ ) {
337
+ if ($ inverse = $ this ->getInverseRelationship ()) {
338
+ $ morphOne ->inverse ($ inverse );
339
+ }
340
+ }
341
+
342
+ return $ morphOne ;
343
+ }, $ relationName );
344
+ }
255
345
};
256
346
}
257
347
0 commit comments