diff --git a/src/Eloquent/CustomRelations/HasCleverRelationships.php b/src/Eloquent/CustomRelations/HasCleverRelationships.php index 3cb2dca..0a970e4 100644 --- a/src/Eloquent/CustomRelations/HasCleverRelationships.php +++ b/src/Eloquent/CustomRelations/HasCleverRelationships.php @@ -15,6 +15,7 @@ use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\MorphToMany; +use MacropaySolutions\LaravelCrudWizard\Models\BaseModel; /** * @see HasRelationships @@ -29,12 +30,17 @@ trait HasCleverRelationships */ protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey): HasOne { - return new class($query, $parent, $foreignKey, $localKey) extends HasOne { + return new class ($query, $parent, $foreignKey, $localKey, $this) extends HasOne { use RelationCleverTrait; - public function __construct(Builder $query, Model $parent, string $foreignKey, string $localKey) - { - $this->setConstraintsStaticFlag($parent); + public function __construct( + Builder $query, + Model $parent, + string $foreignKey, + string $localKey, + BaseModel $resourceModel + ) { + $this->setConstraintsStaticFlag($resourceModel); return parent::__construct($query, $parent, $foreignKey, $localKey); } @@ -53,14 +59,15 @@ protected function newHasOneThrough( $localKey, $secondLocalKey ): HasOneThrough { - return new class( + return new class ( $query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, - $secondLocalKey + $secondLocalKey, + $this ) extends HasOneThrough { use RelationCleverTrait; @@ -71,9 +78,10 @@ public function __construct( string $firstKey, string $secondKey, string $localKey, - string $secondLocalKey + string $secondLocalKey, + BaseModel $resourceModel ) { - $this->setConstraintsStaticFlag($throughParent); + $this->setConstraintsStaticFlag($resourceModel); return parent::__construct( $query, @@ -93,12 +101,18 @@ public function __construct( */ protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey): MorphOne { - return new class($query, $parent, $type, $id, $localKey) extends MorphOne { + return new class ($query, $parent, $type, $id, $localKey, $this) extends MorphOne { use RelationCleverTrait; - public function __construct(Builder $query, Model $parent, string $type, string $id, string $localKey) - { - $this->setConstraintsStaticFlag($parent); + public function __construct( + Builder $query, + Model $parent, + string $type, + string $id, + string $localKey, + BaseModel $resourceModel + ) { + $this->setConstraintsStaticFlag($resourceModel); return parent::__construct($query, $parent, $type, $id, $localKey); } @@ -115,7 +129,7 @@ protected function newBelongsTo( $ownerKey, $relation ): BelongsTo { - return new class($query, $child, $foreignKey, $ownerKey, $relation) extends BelongsTo { + return new class ($query, $child, $foreignKey, $ownerKey, $relation, $this) extends BelongsTo { use RelationCleverTrait; public function __construct( @@ -123,9 +137,10 @@ public function __construct( Model $child, string $foreignKey, string $ownerKey, - string $relation + string $relation, + BaseModel $resourceModel ) { - $this->setConstraintsStaticFlag($child); + $this->setConstraintsStaticFlag($resourceModel); return parent::__construct($query, $child, $foreignKey, $ownerKey, $relation); } @@ -143,7 +158,7 @@ protected function newMorphTo( $type, $relation ): MorphTo { - return new class($query, $parent, $foreignKey, $ownerKey, $type, $relation) extends MorphTo { + return new class ($query, $parent, $foreignKey, $ownerKey, $type, $relation, $this) extends MorphTo { use RelationCleverTrait; public function __construct( @@ -152,9 +167,10 @@ public function __construct( string $foreignKey, string $ownerKey, string $type, - string $relation + string $relation, + BaseModel $resourceModel ) { - $this->setConstraintsStaticFlag($parent); + $this->setConstraintsStaticFlag($resourceModel); return parent::__construct($query, $parent, $foreignKey, $ownerKey, $type, $relation); } @@ -166,12 +182,17 @@ public function __construct( */ protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey): HasMany { - return new class($query, $parent, $foreignKey, $localKey) extends HasMany { + return new class ($query, $parent, $foreignKey, $localKey, $this) extends HasMany { use RelationCleverTrait; - public function __construct(Builder $query, Model $parent, string $foreignKey, string $localKey) - { - $this->setConstraintsStaticFlag($parent); + public function __construct( + Builder $query, + Model $parent, + string $foreignKey, + string $localKey, + BaseModel $resourceModel + ) { + $this->setConstraintsStaticFlag($resourceModel); return parent::__construct($query, $parent, $foreignKey, $localKey); } @@ -190,8 +211,16 @@ protected function newHasManyThrough( $localKey, $secondLocalKey ): HasManyThrough { - return new class($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) extends - HasManyThrough { + return new class ( + $query, + $farParent, + $throughParent, + $firstKey, + $secondKey, + $localKey, + $secondLocalKey, + $this + ) extends HasManyThrough { use RelationCleverTrait; public function __construct( @@ -201,9 +230,10 @@ public function __construct( string $firstKey, string $secondKey, string $localKey, - string $secondLocalKey + string $secondLocalKey, + BaseModel $resourceModel ) { - $this->setConstraintsStaticFlag($throughParent); + $this->setConstraintsStaticFlag($resourceModel); return parent::__construct( $query, @@ -223,12 +253,18 @@ public function __construct( */ protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey): MorphMany { - return new class($query, $parent, $type, $id, $localKey) extends MorphMany { + return new class ($query, $parent, $type, $id, $localKey, $this) extends MorphMany { use RelationCleverTrait; - public function __construct(Builder $query, Model $parent, string $type, string $id, string $localKey) - { - $this->setConstraintsStaticFlag($parent); + public function __construct( + Builder $query, + Model $parent, + string $type, + string $id, + string $localKey, + BaseModel $resourceModel + ) { + $this->setConstraintsStaticFlag($resourceModel); return parent::__construct($query, $parent, $type, $id, $localKey); } @@ -248,8 +284,17 @@ protected function newBelongsToMany( $relatedKey, $relationName = null ): BelongsToMany { - return new class($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName) extends - BelongsToMany { + return new class ( + $query, + $parent, + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $this, + $relationName + ) extends BelongsToMany { use RelationCleverTrait; public function __construct( @@ -260,9 +305,10 @@ public function __construct( string $relatedPivotKey, string $parentKey, string $relatedKey, + BaseModel $resourceModel, ?string $relationName = null ) { - $this->setConstraintsStaticFlag($parent); + $this->setConstraintsStaticFlag($resourceModel); return parent::__construct( $query, @@ -293,7 +339,7 @@ protected function newMorphToMany( $relationName = null, $inverse = false ): MorphToMany { - return new class( + return new class ( $query, $parent, $name, @@ -303,7 +349,8 @@ protected function newMorphToMany( $parentKey, $relatedKey, $relationName, - $inverse + $inverse, + $this ) extends MorphToMany { use RelationCleverTrait; @@ -317,9 +364,10 @@ public function __construct( string $parentKey, string $relatedKey, ?string $relationName, - bool $inverse + bool $inverse, + BaseModel $resourceModel ) { - $this->setConstraintsStaticFlag($parent); + $this->setConstraintsStaticFlag($resourceModel); return parent::__construct( $query,