Skip to content

Commit

Permalink
[9.x] Address Dynamic Relation Resolver inconsiency issue with extend…
Browse files Browse the repository at this point in the history
…ed Models (#45122)

* laravel/framework#44741 - Address Dynamic Relation Resolver inconsistency issue with extended Models

* style fixes
  • Loading branch information
vmcvlad authored Nov 29, 2022
1 parent 9b0df48 commit f56cff6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public function isRelation($key)
}

return method_exists($this, $key) ||
(static::$relationResolvers[get_class($this)][$key] ?? null);
$this->relationResolver(static::class, $key);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions Eloquent/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ trait HasRelationships
*/
protected static $relationResolvers = [];

/**
* Get the dynamic relation resolver if defined or inherited, or return null.
*
* @param string $class
* @param string $key
* @return mixed
*/
public function relationResolver($class, $key)
{
if ($resolver = static::$relationResolvers[$class][$key] ?? null) {
return $resolver;
}

if ($parent = get_parent_class($class)) {
return $this->relationResolver($parent, $key);
}

return null;
}

/**
* Define a dynamic relation resolver.
*
Expand Down
2 changes: 1 addition & 1 deletion Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2314,7 +2314,7 @@ public function __call($method, $parameters)
return $this->$method(...$parameters);
}

if ($resolver = (static::$relationResolvers[get_class($this)][$method] ?? null)) {
if ($resolver = ($this->relationResolver(static::class, $method))) {
return $resolver($this);
}

Expand Down

0 comments on commit f56cff6

Please sign in to comment.