Skip to content

isRelation checks only if a method exists but not if a relation is returned #51592

Closed
@denny-bee

Description

@denny-bee

Laravel Version

10

PHP Version

8.2

Database Driver & Version

MySql

Description

HasAttributes.php has the method isRelation.

This method only checks if there is no mutator with the same name and if there is a method with this name.

But if you have some helper methods which are not accessors ("getXyzAttribute" or "xyz(): Attribute"), it will still say it is an relation, which is not correct.

In my oppinion there should be a check for the return type like it is used in "hasAttributeMutator":

$returnType = (new ReflectionMethod($this, $method))->getReturnType();

return static::$attributeMutatorCache[get_class($this)][$key] =
                    $returnType instanceof ReflectionNamedType &&
                    $returnType->getName() === Attribute::class;

If there are some helper methods which are not relations but return the same type, they could be defined in a property and excluded by this check to support third party packages and custom code.

"Illuminate\Database\Eloquent\Relations\Relation" could be the type instead of Attribute::class in this case.

But with is_a instead of a basic check to include classes which extend Relation::class.

Or is there any other reason why this was not implemented this way?

Steps To Reproduce

  1. Create an model
  2. Add a helper method e.g. pizza() this model
  3. isRelation('pizza') on a object of this model would result in true, but it is not an relation

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions