Skip to content

Commit e8ba477

Browse files
author
Admin
committed
Fix eager loading for MorphMany::one and HasMany::one methods laravel/framework#51825
1 parent 9ce90cb commit e8ba477

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

src/Eloquent/CustomRelations/HasCleverRelationships.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Illuminate\Database\Eloquent\Relations\MorphOne;
1616
use Illuminate\Database\Eloquent\Relations\MorphTo;
1717
use Illuminate\Database\Eloquent\Relations\MorphToMany;
18+
use Illuminate\Database\Eloquent\Relations\Relation;
19+
use Illuminate\Support\Str;
1820
use MacropaySolutions\LaravelCrudWizard\Models\BaseModel;
1921

2022
/**
@@ -184,6 +186,37 @@ public function __construct(
184186

185187
return parent::__construct($query, $parent, $foreignKey, $localKey);
186188
}
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+
}
187220
};
188221
}
189222

@@ -231,6 +264,31 @@ public function __construct(
231264
$secondLocalKey
232265
);
233266
}
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+
}
234292
};
235293
}
236294

@@ -252,6 +310,38 @@ public function __construct(
252310

253311
return parent::__construct($query, $parent, $type, $id, $localKey);
254312
}
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+
}
255345
};
256346
}
257347

0 commit comments

Comments
 (0)