diff --git a/src/Eloquent/CustomRelations/Builders/CleverEloquentBuilder.php b/src/Eloquent/CustomRelations/Builders/CleverEloquentBuilder.php index f5d69a4..ce6a417 100644 --- a/src/Eloquent/CustomRelations/Builders/CleverEloquentBuilder.php +++ b/src/Eloquent/CustomRelations/Builders/CleverEloquentBuilder.php @@ -7,7 +7,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\Relation; -use MacropaySolutions\LaravelCrudWizard\Eloquent\CustomRelations\RelationCleverTrait; use MacropaySolutions\LaravelCrudWizard\Models\BaseModel; class CleverEloquentBuilder extends Builder @@ -17,7 +16,7 @@ class CleverEloquentBuilder extends Builder */ public function getRelation($name): Relation { - $relation = RelationCleverTrait::noConstraints(function () use ($name): Relation { + $relation = Relation::macroNoConstraints(function () use ($name): Relation { try { $model = $this->getModel()->newInstance(); /** @var BaseModel $model */ @@ -43,7 +42,7 @@ public function getRelation($name): Relation */ protected function getRelationWithoutConstraints($relation): Relation { - return RelationCleverTrait::noConstraints(function () use ($relation): Relation { + return Relation::macroNoConstraints(function () use ($relation): Relation { $model = $this->getModel(); /** @var BaseModel $model */ $model->nowEagerLoadingRelationNameWithNoConstraints = $relation; @@ -57,8 +56,8 @@ protected function getRelationWithoutConstraints($relation): Relation */ protected function getBelongsToRelation(MorphTo $relation, $type): BelongsTo { - /** this will work as before, the static property from Relation is overwritten in RelationCleverTrait */ - $belongsTo = RelationCleverTrait::noConstraints(function () use ($relation, $type): Relation { + /** this will work as before */ + $belongsTo = Relation::macroNoConstraints(function () use ($relation, $type): Relation { return $this->model->belongsTo( $type, $relation->getForeignKeyName(), diff --git a/src/Eloquent/CustomRelations/HasCleverRelationships.php b/src/Eloquent/CustomRelations/HasCleverRelationships.php index 0a970e4..98b2f90 100644 --- a/src/Eloquent/CustomRelations/HasCleverRelationships.php +++ b/src/Eloquent/CustomRelations/HasCleverRelationships.php @@ -31,8 +31,6 @@ trait HasCleverRelationships protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey): HasOne { return new class ($query, $parent, $foreignKey, $localKey, $this) extends HasOne { - use RelationCleverTrait; - public function __construct( Builder $query, Model $parent, @@ -69,8 +67,6 @@ protected function newHasOneThrough( $secondLocalKey, $this ) extends HasOneThrough { - use RelationCleverTrait; - public function __construct( Builder $query, Model $farParent, @@ -102,8 +98,6 @@ public function __construct( protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey): MorphOne { return new class ($query, $parent, $type, $id, $localKey, $this) extends MorphOne { - use RelationCleverTrait; - public function __construct( Builder $query, Model $parent, @@ -130,8 +124,6 @@ protected function newBelongsTo( $relation ): BelongsTo { return new class ($query, $child, $foreignKey, $ownerKey, $relation, $this) extends BelongsTo { - use RelationCleverTrait; - public function __construct( Builder $query, Model $child, @@ -159,8 +151,6 @@ protected function newMorphTo( $relation ): MorphTo { return new class ($query, $parent, $foreignKey, $ownerKey, $type, $relation, $this) extends MorphTo { - use RelationCleverTrait; - public function __construct( Builder $query, Model $parent, @@ -183,8 +173,6 @@ public function __construct( protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey): HasMany { return new class ($query, $parent, $foreignKey, $localKey, $this) extends HasMany { - use RelationCleverTrait; - public function __construct( Builder $query, Model $parent, @@ -221,8 +209,6 @@ protected function newHasManyThrough( $secondLocalKey, $this ) extends HasManyThrough { - use RelationCleverTrait; - public function __construct( Builder $query, Model $farParent, @@ -254,8 +240,6 @@ public function __construct( protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey): MorphMany { return new class ($query, $parent, $type, $id, $localKey, $this) extends MorphMany { - use RelationCleverTrait; - public function __construct( Builder $query, Model $parent, @@ -295,8 +279,6 @@ protected function newBelongsToMany( $this, $relationName ) extends BelongsToMany { - use RelationCleverTrait; - public function __construct( Builder $query, Model $parent, @@ -352,8 +334,6 @@ protected function newMorphToMany( $inverse, $this ) extends MorphToMany { - use RelationCleverTrait; - public function __construct( Builder $query, Model $parent, diff --git a/src/Eloquent/CustomRelations/RelationCleverTrait.php b/src/Eloquent/CustomRelations/RelationCleverTrait.php deleted file mode 100644 index 5ec614b..0000000 --- a/src/Eloquent/CustomRelations/RelationCleverTrait.php +++ /dev/null @@ -1,71 +0,0 @@ -nowEagerLoadingRelationNameWithNoConstraints - ) { - return; - } - - /** - * 1st execution is for ExampleModel $exampleModel on 'rel' relation - * with nowEagerLoadingRelationNameWithNoConstraints = 'rel' - * and with $noConstraintsForRelationName = 'rel' - */ - /* 2nd execution is for UserModel $userModel on 'categories' relation - with nowEagerLoadingRelationNameWithNoConstraints = null - and with $noConstraintsForRelationName = 'rel' */ - - - /* 1st execution is for ExampleModel $exampleModel on 'children' relation - with nowEagerLoadingRelationNameWithNoConstraints = null - and with $noConstraintsForRelationName = 'rel' */ - /** - * 2nd execution is for ExampleModel $exampleModel on 'rel' relation - * with nowEagerLoadingRelationNameWithNoConstraints = 'rel' - * and with $noConstraintsForRelationName = 'rel' - */ - /* 3rd execution is for UserModel $userModel on 'categories' relation - with nowEagerLoadingRelationNameWithNoConstraints = null - and with $noConstraintsForRelationName = 'rel' */ - static::$constraints = - RelationCleverTrait::$noConstraintsForRelationName !== $model->nowEagerLoadingRelationNameWithNoConstraints; - } -} \ No newline at end of file diff --git a/src/Providers/CrudProvider.php b/src/Providers/CrudProvider.php index 55d4050..e9d8f4f 100644 --- a/src/Providers/CrudProvider.php +++ b/src/Providers/CrudProvider.php @@ -4,8 +4,10 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Http\Request; use Illuminate\Support\ServiceProvider; +use MacropaySolutions\LaravelCrudWizard\Models\BaseModel; class CrudProvider extends ServiceProvider { @@ -58,5 +60,66 @@ function (...$arguments): Model { return $this; }); + + Relation::macro('noConstraintsForRelationName', function (?string $relation = null): ?string { + global $noConstraintsForRelationName; + + if (\func_num_args() === 0) { + return $noConstraintsForRelationName ?? null; + } + + return $noConstraintsForRelationName = $relation; + }); + + Relation::macro('macroNoConstraints', function (\Closure $callback, ?string $relationName = null): mixed { + $previous = static::$constraints; + $previousNoConstraintsForRelationName = Relation::noConstraintsForRelationName(); + + if ('' !== (string)$relationName) { + Relation::noConstraintsForRelationName($relationName); + } else { + static::$constraints = false; + } + + try { + return $callback(); + } finally { + static::$constraints = $previous; + Relation::noConstraintsForRelationName($previousNoConstraintsForRelationName); + } + }); + + Relation::macro('setConstraintsStaticFlag', function (BaseModel $model): void { + if ( + '' === (string)Relation::noConstraintsForRelationName() + && '' === (string)$model->nowEagerLoadingRelationNameWithNoConstraints + ) { + return; + } + + /** + * 1st execution is for ExampleModel $exampleModel on 'rel' relation + * with nowEagerLoadingRelationNameWithNoConstraints = 'rel' + * and with $noConstraintsForRelationName = 'rel' + */ + /* 2nd execution is for UserModel $userModel on 'categories' relation + with nowEagerLoadingRelationNameWithNoConstraints = null + and with $noConstraintsForRelationName = 'rel' */ + + + /* 1st execution is for ExampleModel $exampleModel on 'children' relation + with nowEagerLoadingRelationNameWithNoConstraints = null + and with $noConstraintsForRelationName = 'rel' */ + /** + * 2nd execution is for ExampleModel $exampleModel on 'rel' relation + * with nowEagerLoadingRelationNameWithNoConstraints = 'rel' + * and with $noConstraintsForRelationName = 'rel' + */ + /* 3rd execution is for UserModel $userModel on 'categories' relation + with nowEagerLoadingRelationNameWithNoConstraints = null + and with $noConstraintsForRelationName = 'rel' */ + static::$constraints = + Relation::noConstraintsForRelationName() !== $model->nowEagerLoadingRelationNameWithNoConstraints; + }); } }