From b43a466e05576569748e85720bf76dc0b37421dd Mon Sep 17 00:00:00 2001 From: Marius Date: Thu, 21 Nov 2024 15:22:00 +0200 Subject: [PATCH] fix eager loading retroactively https://github.com/laravel/framework/issues/51825 --- .../HasCleverRelationships.php | 109 ++++++++++++------ 1 file changed, 74 insertions(+), 35 deletions(-) diff --git a/src/Eloquent/CustomRelations/HasCleverRelationships.php b/src/Eloquent/CustomRelations/HasCleverRelationships.php index 59b0b05..3cb2dca 100644 --- a/src/Eloquent/CustomRelations/HasCleverRelationships.php +++ b/src/Eloquent/CustomRelations/HasCleverRelationships.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Concerns\HasRelationships; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -14,7 +15,6 @@ use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\MorphToMany; -use MacropaySolutions\LaravelCrudWizard\Models\BaseModel as Model; /** * @see HasRelationships @@ -24,7 +24,10 @@ trait HasCleverRelationships { public ?string $nowEagerLoadingRelationNameWithNoConstraints = null; - protected function newHasOne(Builder $query, Model $parent, string $foreignKey, string $localKey): HasOne + /** + * @inheritDoc + */ + protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey): HasOne { return new class($query, $parent, $foreignKey, $localKey) extends HasOne { use RelationCleverTrait; @@ -38,14 +41,17 @@ public function __construct(Builder $query, Model $parent, string $foreignKey, s }; } + /** + * @inheritDoc + */ protected function newHasOneThrough( Builder $query, Model $farParent, Model $throughParent, - string $firstKey, - string $secondKey, - string $localKey, - string $secondLocalKey + $firstKey, + $secondKey, + $localKey, + $secondLocalKey ): HasOneThrough { return new class( $query, @@ -82,7 +88,10 @@ public function __construct( }; } - protected function newMorphOne(Builder $query, Model $parent, string $type, string $id, string $localKey): MorphOne + /** + * @inheritDoc + */ + protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey): MorphOne { return new class($query, $parent, $type, $id, $localKey) extends MorphOne { use RelationCleverTrait; @@ -96,12 +105,15 @@ public function __construct(Builder $query, Model $parent, string $type, string }; } + /** + * @inheritDoc + */ protected function newBelongsTo( Builder $query, Model $child, - string $foreignKey, - string $ownerKey, - string $relation + $foreignKey, + $ownerKey, + $relation ): BelongsTo { return new class($query, $child, $foreignKey, $ownerKey, $relation) extends BelongsTo { use RelationCleverTrait; @@ -120,13 +132,16 @@ public function __construct( }; } + /** + * @inheritDoc + */ protected function newMorphTo( Builder $query, Model $parent, - string $foreignKey, - string $ownerKey, - string $type, - string $relation + $foreignKey, + $ownerKey, + $type, + $relation ): MorphTo { return new class($query, $parent, $foreignKey, $ownerKey, $type, $relation) extends MorphTo { use RelationCleverTrait; @@ -146,7 +161,10 @@ public function __construct( }; } - protected function newHasMany(Builder $query, Model $parent, string $foreignKey, string $localKey): HasMany + /** + * @inheritDoc + */ + protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey): HasMany { return new class($query, $parent, $foreignKey, $localKey) extends HasMany { use RelationCleverTrait; @@ -160,14 +178,17 @@ public function __construct(Builder $query, Model $parent, string $foreignKey, s }; } + /** + * @inheritDoc + */ protected function newHasManyThrough( Builder $query, Model $farParent, Model $throughParent, - string $firstKey, - string $secondKey, - string $localKey, - string $secondLocalKey + $firstKey, + $secondKey, + $localKey, + $secondLocalKey ): HasManyThrough { return new class($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) extends HasManyThrough { @@ -197,6 +218,9 @@ public function __construct( }; } + /** + * @inheritDoc + */ protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey): MorphMany { return new class($query, $parent, $type, $id, $localKey) extends MorphMany { @@ -211,15 +235,18 @@ public function __construct(Builder $query, Model $parent, string $type, string }; } + /** + * @inheritDoc + */ protected function newBelongsToMany( Builder $query, Model $parent, - string $table, - string $foreignPivotKey, - string $relatedPivotKey, - string $parentKey, - string $relatedKey, - ?string $relationName = null + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $relationName = null ): BelongsToMany { return new class($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName) extends BelongsToMany { @@ -251,20 +278,32 @@ public function __construct( }; } + /** + * @inheritDoc + */ protected function newMorphToMany( Builder $query, Model $parent, - string $name, - string $table, - string $foreignPivotKey, - string $relatedPivotKey, - string $parentKey, - string $relatedKey, - ?string $relationName = null, - bool $inverse = false + $name, + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $relationName = null, + $inverse = false ): MorphToMany { - return new class($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, - $relationName, $inverse + return new class( + $query, + $parent, + $name, + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $relationName, + $inverse ) extends MorphToMany { use RelationCleverTrait;