Skip to content

Commit

Permalink
Access the $manyMethods property through static::$manyMethods
Browse files Browse the repository at this point in the history
This allows adding additional $manyMethods when extending the model class
and still receive a correct guess for the relationship name.

Removed the name of guessBelongsToManyRelation from the list but still consider
it when analyzing the call stack. This makes $manyMethods more slim and easier
to override.

Removed findFirstMethodThatIsntRelation from $hasMany array, since it does not
appear anywhere in the code and seems to have been forgotten there.
It is also not really a name for a many-relationship method, so if it is needed,
i think there might be a better place for it.
  • Loading branch information
spawnia committed Oct 30, 2018
1 parent 96d4ece commit 7cef687
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ trait HasRelationships
*/
public static $manyMethods = [
'belongsToMany', 'morphToMany', 'morphedByMany',
'guessBelongsToManyRelation', 'findFirstMethodThatIsntRelation',
];

/**
Expand Down Expand Up @@ -546,14 +545,19 @@ public function morphedByMany($related, $name, $table = null, $foreignPivotKey =
}

/**
* Get the relationship name of the belongs to many.
* Get the relationship name of the belongsToMany.
*
* @return string
* @return string|null
*/
protected function guessBelongsToManyRelation()
{
// Search the call stack for the name of the relationship defined in the model
$caller = Arr::first(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), function ($trace) {
return ! in_array($trace['function'], Model::$manyMethods);
// Exclude names of the relationship methods and this method itself
return ! in_array(
$trace['function'],
array_merge(static::$manyMethods, ['guessBelongsToManyRelation'])
);
});

return ! is_null($caller) ? $caller['function'] : null;
Expand Down

0 comments on commit 7cef687

Please sign in to comment.