Skip to content

Commit

Permalink
Merge pull request #1 from biiiiiigmonster/fix-instanceof
Browse files Browse the repository at this point in the history
Fix instanceof
  • Loading branch information
27pchrisl authored Mar 16, 2024
2 parents d6a96b1 + cc9c1a2 commit 6f5d686
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions src/Database/Eloquent/RelationMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
Expand All @@ -23,6 +22,7 @@ public function getRelationExistenceInQuery(): Closure
$relation = function (Builder $query, Builder $parentQuery, $columns = ['*']): Builder {
return $query->select($columns);
};
// basic builder
$belongsTo = function (Builder $query, Builder $parentQuery) use ($relation): Builder {
$columns = $query->qualifyColumn($this->ownerKey);

Expand Down Expand Up @@ -62,32 +62,6 @@ public function getRelationExistenceInQuery(): Closure

return $relation($query, $parentQuery, $columns);
};
$hasOne = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder {
if ($this->isOneOfMany()) {
$this->mergeOneOfManyJoinsTo($query);
}

return $hasOneOrMany($query, $parentQuery);
};
$morphOneOrMany = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder {
return $hasOneOrMany($query, $parentQuery)->where(
$query->qualifyColumn($this->getMorphType()),
$this->morphClass
);
};
$morphOne = function (Builder $query, Builder $parentQuery) use ($morphOneOrMany): Builder {
if ($this->isOneOfMany()) {
$this->mergeOneOfManyJoinsTo($query);
}

return $morphOneOrMany($query, $parentQuery);
};
$morphToMany = function (Builder $query, Builder $parentQuery) use ($belongsToMany): Builder {
return $belongsToMany($query, $parentQuery)->where(
$this->qualifyPivotColumn($this->morphType),
$this->morphClass
);
};
$hasManyThrough = function (Builder $query, Builder $parentQuery) use ($relation): Builder {
$columns = $this->getQualifiedFirstKeyName();
if ($parentQuery->getQuery()->from === $query->getQuery()->from) {
Expand Down Expand Up @@ -120,16 +94,45 @@ public function getRelationExistenceInQuery(): Closure

return $relation($query, $parentQuery, $columns);
};
// extended builder
$hasOne = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder {
if ($this->isOneOfMany()) {
$this->mergeOneOfManyJoinsTo($query);
}

return $hasOneOrMany($query, $parentQuery);
};
$morphOneOrMany = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder {
return $hasOneOrMany($query, $parentQuery)->where(
$query->qualifyColumn($this->getMorphType()),
$this->morphClass
);
};
$morphOne = function (Builder $query, Builder $parentQuery) use ($morphOneOrMany): Builder {
if ($this->isOneOfMany()) {
$this->mergeOneOfManyJoinsTo($query);
}

return $morphOneOrMany($query, $parentQuery);
};
$morphToMany = function (Builder $query, Builder $parentQuery) use ($belongsToMany): Builder {
return $belongsToMany($query, $parentQuery)->where(
$this->qualifyPivotColumn($this->morphType),
$this->morphClass
);
};

return match (true) {
// extended relation
$this instanceof HasOne => $hasOne($query, $parentQuery),
$this instanceof MorphOne => $morphOne($query, $parentQuery),
$this instanceof MorphOneOrMany => $morphOneOrMany($query, $parentQuery),
$this instanceof BelongsTo => $belongsTo($query, $parentQuery),
$this instanceof MorphToMany => $morphToMany($query, $parentQuery),
// basic relation
$this instanceof BelongsTo => $belongsTo($query, $parentQuery),
$this instanceof BelongsToMany => $belongsToMany($query, $parentQuery),
$this instanceof HasOneOrMany => $hasOneOrMany($query, $parentQuery),
$this instanceof HasOne => $hasOne($query, $parentQuery),
$this instanceof HasManyThrough => $hasManyThrough($query, $parentQuery),
$this instanceof BelongsToMany => $belongsToMany($query, $parentQuery),
default => throw new LogicException(
sprintf('%s must be a relationship instance.', $this::class)
)
Expand All @@ -142,7 +145,7 @@ public function getRelationWhereInKey(): Closure
return fn (): string => match (true) {
$this instanceof BelongsTo => $this->getQualifiedForeignKeyName(),
$this instanceof HasOneOrMany, $this instanceof BelongsToMany => $this->getQualifiedParentKeyName(),
$this instanceof HasOneThrough => $this->getQualifiedLocalKeyName(),
$this instanceof HasManyThrough => $this->getQualifiedLocalKeyName(),
default => throw new LogicException(
sprintf('%s must be a relationship instance.', $this::class)
)
Expand Down

0 comments on commit 6f5d686

Please sign in to comment.