Skip to content

Commit 7cef687

Browse files
committed
Access the $manyMethods property through static::$manyMethods
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.
1 parent 96d4ece commit 7cef687

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ trait HasRelationships
4141
*/
4242
public static $manyMethods = [
4343
'belongsToMany', 'morphToMany', 'morphedByMany',
44-
'guessBelongsToManyRelation', 'findFirstMethodThatIsntRelation',
4544
];
4645

4746
/**
@@ -546,14 +545,19 @@ public function morphedByMany($related, $name, $table = null, $foreignPivotKey =
546545
}
547546

548547
/**
549-
* Get the relationship name of the belongs to many.
548+
* Get the relationship name of the belongsToMany.
550549
*
551-
* @return string
550+
* @return string|null
552551
*/
553552
protected function guessBelongsToManyRelation()
554553
{
554+
// Search the call stack for the name of the relationship defined in the model
555555
$caller = Arr::first(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), function ($trace) {
556-
return ! in_array($trace['function'], Model::$manyMethods);
556+
// Exclude names of the relationship methods and this method itself
557+
return ! in_array(
558+
$trace['function'],
559+
array_merge(static::$manyMethods, ['guessBelongsToManyRelation'])
560+
);
557561
});
558562

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

0 commit comments

Comments
 (0)