Skip to content

Commit

Permalink
fix eager loading retroactively laravel/framework#51825
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius committed Nov 21, 2024
1 parent aae9bfc commit b43a466
Showing 1 changed file with 74 additions and 35 deletions.
109 changes: 74 additions & 35 deletions src/Eloquent/CustomRelations/HasCleverRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit b43a466

Please sign in to comment.