|
4 | 4 |
|
5 | 5 | use Kirschbaum\PowerJoins\StaticCache; |
6 | 6 | use Kirschbaum\PowerJoins\PowerJoinClause; |
| 7 | +use Kirschbaum\PowerJoins\Tests\Models\Post; |
7 | 8 | use Illuminate\Database\Eloquent\SoftDeletes; |
8 | 9 | use Illuminate\Database\Eloquent\Relations\HasOne; |
9 | 10 | use Illuminate\Database\Eloquent\Relations\HasMany; |
| 11 | +use Illuminate\Database\Eloquent\Relations\MorphTo; |
10 | 12 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 13 | +use Illuminate\Database\Eloquent\Relations\MorphToMany; |
11 | 14 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
12 | 15 | use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
13 | 16 | use Illuminate\Database\Eloquent\Relations\MorphOneOrMany; |
14 | | -use Illuminate\Database\Eloquent\Relations\MorphToMany; |
15 | 17 |
|
16 | 18 | /** |
17 | 19 | * @method \Illuminate\Database\Eloquent\Model getModel() |
@@ -49,20 +51,16 @@ class RelationshipsExtraMethods |
49 | 51 | */ |
50 | 52 | public function performJoinForEloquentPowerJoins() |
51 | 53 | { |
52 | | - return function ($builder, $joinType = 'leftJoin', $callback = null, $alias = null, bool $disableExtraConditions = false) { |
53 | | - if ($this instanceof MorphToMany) { |
54 | | - return $this->performJoinForEloquentPowerJoinsForMorphToMany($builder, $joinType, $callback, $alias, $disableExtraConditions); |
55 | | - } elseif ($this instanceof BelongsToMany) { |
56 | | - return $this->performJoinForEloquentPowerJoinsForBelongsToMany($builder, $joinType, $callback, $alias, $disableExtraConditions); |
57 | | - } elseif ($this instanceof MorphOneOrMany) { |
58 | | - return $this->performJoinForEloquentPowerJoinsForMorph($builder, $joinType, $callback, $alias, $disableExtraConditions); |
59 | | - } elseif ($this instanceof HasMany || $this instanceof HasOne) { |
60 | | - return $this->performJoinForEloquentPowerJoinsForHasMany($builder, $joinType, $callback, $alias, $disableExtraConditions); |
61 | | - } elseif ($this instanceof HasManyThrough) { |
62 | | - return $this->performJoinForEloquentPowerJoinsForHasManyThrough($builder, $joinType, $callback, $alias, $disableExtraConditions); |
63 | | - } else { |
64 | | - return $this->performJoinForEloquentPowerJoinsForBelongsTo($builder, $joinType, $callback, $alias, $disableExtraConditions); |
65 | | - } |
| 54 | + return function ($builder, $joinType = 'leftJoin', $callback = null, $alias = null, bool $disableExtraConditions = false, string $morphable = null) { |
| 55 | + return match (true) { |
| 56 | + $this instanceof MorphToMany => $this->performJoinForEloquentPowerJoinsForMorphToMany($builder, $joinType, $callback, $alias, $disableExtraConditions), |
| 57 | + $this instanceof BelongsToMany => $this->performJoinForEloquentPowerJoinsForBelongsToMany($builder, $joinType, $callback, $alias, $disableExtraConditions), |
| 58 | + $this instanceof MorphOneOrMany => $this->performJoinForEloquentPowerJoinsForMorph($builder, $joinType, $callback, $alias, $disableExtraConditions), |
| 59 | + $this instanceof HasMany || $this instanceof HasOne => $this->performJoinForEloquentPowerJoinsForHasMany($builder, $joinType, $callback, $alias, $disableExtraConditions), |
| 60 | + $this instanceof HasManyThrough => $this->performJoinForEloquentPowerJoinsForHasManyThrough($builder, $joinType, $callback, $alias, $disableExtraConditions), |
| 61 | + $this instanceof MorphTo => $this->performJoinForEloquentPowerJoinsForMorphTo($builder, $joinType, $callback, $alias, $disableExtraConditions, $morphable), |
| 62 | + default => $this->performJoinForEloquentPowerJoinsForBelongsTo($builder, $joinType, $callback, $alias, $disableExtraConditions), |
| 63 | + }; |
66 | 64 | }; |
67 | 65 | } |
68 | 66 |
|
@@ -243,6 +241,38 @@ protected function performJoinForEloquentPowerJoinsForMorph() |
243 | 241 | }; |
244 | 242 | } |
245 | 243 |
|
| 244 | + /** |
| 245 | + * Perform the JOIN clause for when calling the morphTo method from the morphable class. |
| 246 | + */ |
| 247 | + protected function performJoinForEloquentPowerJoinsForMorphTo() |
| 248 | + { |
| 249 | + return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false, string $morphable = null) { |
| 250 | + $modelInstance = new $morphable; |
| 251 | + |
| 252 | + $builder->{$joinType}($modelInstance->getTable(), function ($join) use ($modelInstance, $callback, $disableExtraConditions) { |
| 253 | + $join->on( |
| 254 | + "{$this->getModel()->getTable()}.{$this->getForeignKeyName()}", |
| 255 | + '=', |
| 256 | + "{$modelInstance->getTable()}.{$modelInstance->getKeyName()}" |
| 257 | + )->where("{$this->getModel()->getTable()}.{$this->getMorphType()}", '=', $modelInstance->getMorphClass()); |
| 258 | + |
| 259 | + if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getModel())) { |
| 260 | + $join->whereNull($this->query->getModel()->getQualifiedDeletedAtColumn()); |
| 261 | + } |
| 262 | + |
| 263 | + if ($disableExtraConditions === false) { |
| 264 | + $this->applyExtraConditions($join); |
| 265 | + } |
| 266 | + |
| 267 | + if ($callback && is_callable($callback)) { |
| 268 | + $callback($join); |
| 269 | + } |
| 270 | + }, $this->getModel()); |
| 271 | + |
| 272 | + return $this; |
| 273 | + }; |
| 274 | + } |
| 275 | + |
246 | 276 | /** |
247 | 277 | * Perform the JOIN clause for the HasMany (or similar) relationships. |
248 | 278 | */ |
|
0 commit comments