Framework: Any reason why the getOriginal function isn't using the cast attribute feature?

Created on 9 Feb 2016  路  7Comments  路  Source: laravel/framework

The getAttributeValue function located in src/Illuminate/Database/Eloquent/Model.php is calling the castAttribute function before returning the value. I was wondering if there was any reason why the getOriginal one wasn't calling it as well so the values from the two functions could be similar or at least the same type.

/**
 * Get a plain attribute (not a relationship).
 *
 * @param  string  $key
 * @return mixed
 */
public function getAttributeValue($key)
{
    $value = $this->getAttributeFromArray($key);

    // If the attribute has a get mutator, we will call that then return what
    // it returns as the value, which is useful for transforming values on
    // retrieval from the model to a form that is more useful for usage.
    if ($this->hasGetMutator($key)) {
        return $this->mutateAttribute($key, $value);
    }

    // If the attribute exists within the cast array, we will convert it to
    // an appropriate native PHP type dependant upon the associated value
    // given with the key in the pair. Dayle made this comment line up.
    if ($this->hasCast($key)) {
        $value = $this->castAttribute($key, $value);
    }

    // If the attribute is listed as a date, we will convert it to a DateTime
    // instance on retrieval, which makes it quite convenient to work with
    // date fields without having to create a mutator for each property.
    elseif (in_array($key, $this->getDates())) {
        if (! is_null($value)) {
            return $this->asDateTime($value);
        }
    }

    return $value;
}
/**
 * Get the model's original attribute values.
 *
 * @param  string|null  $key
 * @param  mixed  $default
 * @return array
 */
public function getOriginal($key = null, $default = null)
{
    return Arr::get($this->original, $key, $default);
}

Most helpful comment

I've lost like 4 hours of my time with this problem. Why are different get functions not following the same standards? If Laravel docs tells you that putting the $casts attribute makes the job for you, it should make the job for you. Cannot be the case that getAttributeValue "makes the work for you" but getOriginal doesn't make the job for you. Don't you think so?

All 7 comments

I think the name is confusing since original values are most of the time the values before mutation that you would expect to be returned from getAttributeValue. Maybe this could be renamed getRawValue instead.

@GrahamCampbell What do you think? Same for getAttributes() and getRawAttributes().

We're open to PRs. They will allow us to verify the intended behaviour from a concrete review. Thank you everyone! :heart:

@GrahamCampbell working on it. Thanks!

This issue hasn't been mentioned since, anyone tried this? I think both options should be available, i've had the need for both multiple times.

edit: And I just noticed for example setAttribute also ignores the 'casts' array, but that's another issue. Just fyi

I've lost like 4 hours of my time with this problem. Why are different get functions not following the same standards? If Laravel docs tells you that putting the $casts attribute makes the job for you, it should make the job for you. Cannot be the case that getAttributeValue "makes the work for you" but getOriginal doesn't make the job for you. Don't you think so?

PR for this: #31470

Was this page helpful?
0 / 5 - 0 ratings