eloquence icon indicating copy to clipboard operation
eloquence copied to clipboard

Logic reversed in getAttribute()

Open valorin opened this issue 7 years ago • 3 comments

I've just noticed an interesting discrepancy in getAttribute() between how the CamelCasing trait works and the original Eloquent Model.

Consider the Eloquent Model function:

public function getAttribute($key)
{
    if (array_key_exists($key, $this->attributes) || $this->hasGetMutator($key)) {
        return $this->getAttributeValue($key);
    }
    return $this->getRelationValue($key);
}

Now look at CamelCasing:

public function getAttribute($key)
{
    if (method_exists($this, $key)) {
        return $this->getRelationValue($key);
    }
    return parent::getAttribute($this->getSnakeKey($key));
}

Eloquent allows attributes (even read-only ones) precedence over relations. CamelCasing favours relationships first.

It seems to me that following Eloquence's order of preference would be a good idea, so the behaviour is consistent between both.

valorin avatar Jul 29 '16 11:07 valorin

@valorin cheers! i have a feeling this may have been changed in the core codebase, as I'm fairly certain I duplicated the first bit of it. Something in the back of my mind is saying that I felt it was weird to begin with (placing relations over attributes).

Happy to change this - it should definitely maintain the same behaviour as Eloquent.

kirkbushell avatar Aug 01 '16 13:08 kirkbushell

any chance this will be updated?

bicatu avatar Sep 12 '17 22:09 bicatu

Certainly, soon as I have time.

kirkbushell avatar Sep 13 '17 00:09 kirkbushell