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 Dec 3, 2024
1 parent ef3d94d commit b0b9618
Showing 1 changed file with 85 additions and 37 deletions.
122 changes: 85 additions & 37 deletions src/Eloquent/CustomRelations/HasCleverRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand All @@ -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;

Expand All @@ -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,
Expand All @@ -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);
}
Expand All @@ -115,17 +129,18 @@ 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(
Builder $query,
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);
}
Expand All @@ -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(
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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);
}
Expand All @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -293,7 +339,7 @@ protected function newMorphToMany(
$relationName = null,
$inverse = false
): MorphToMany {
return new class(
return new class (
$query,
$parent,
$name,
Expand All @@ -303,7 +349,8 @@ protected function newMorphToMany(
$parentKey,
$relatedKey,
$relationName,
$inverse
$inverse,
$this
) extends MorphToMany {
use RelationCleverTrait;

Expand All @@ -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,
Expand Down

0 comments on commit b0b9618

Please sign in to comment.