80

I got a model called Run which contains this method:

public function getNameAttribute($name){
    if($name == 'Eendaags')
        return $this->race_edition->race->name;

    return $this->race_edition->race->name.' '.$name;
}

I need this setup for laravel administrator, since alot of runs will have the same name and the only difference is the race name. But in 1 place in the website i need to get the name only, without mutating. Is this possbile?

2
  • Could you switch to using a getNameRaceAttribute function, intended to only be used by administrator, and set administrator to use the ->name_race field, then ->name would be as it normally is. There is no "Get Raw Attribute" function in eloquent. relevant code: github.com/laravel/framework/blob/master/src/Illuminate/… Jul 9, 2013 at 9:01
  • Tried this but it will bring other problems with it. I guess i need to hack it in administrator.
    – LHolleman
    Jul 9, 2013 at 10:04

4 Answers 4

188

this is the correct way

// that skips mutators
$model->getOriginal('name');

https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Model.html#method_getOriginal

Edit: Careful!

As Maksym Cierzniak explained in the comments, getOriginal() doesn't just skip mutators, it also returns the "original" value of the field at the time the object was read from the database. So if you have since modified the model's property, this won't return your modified value, it will still return the original value. The more consistent and reliable way to get the un-mutated value from within the model class is to retrieve it from the attributes property like this:

$this->attributes['name']

But be aware that attributes is a protected property, so you can't do that from outside the model class. In that case, you can use

$model->getAttributes()['name']`

or Maksym's technique from his comment below.

5
  • 1
    Awesome!! This is exactly what I need, thanks so much. Wish this was inside the documentation. Jan 19, 2015 at 16:32
  • 20
    @SteveBauman One problem you may encounter is that getOriginal() returns the original value returned from database. So if you have name=something and you will change it and call getOriginal('name') you will still get the something value. So you may better grab the value like $name = array_get($model->getAttributes(), 'name'); This way you will be sure that you have the fresh value.
    – Maksym
    Jul 2, 2015 at 8:59
  • 2
    getOriginal() is a great function for using in Observers to check what the new value was versus the old Jul 6, 2018 at 4:22
  • 1
    getOriginal wont work if it does not come from the database already. However, getAttributes() work! Mar 16, 2022 at 14:14
  • 1
    As of Laravel 7 you should use getRawOriginal instead or getOriginal
    – George
    May 8 at 9:31
67

since Laravel 7.x and 8.x there is a new approach to access intact attribute:

// that skips accessors
$model->getRawOriginal('name');

https://laracasts.com/discuss/channels/testing/how-to-disable-casting-during-testing

4
  • 5
    Yes since Laravel 7.x getOriginal() will respect casts and accessors. getRawOriginal() will give the raw original value. Aug 19, 2020 at 7:13
  • 4
    it work for me, in 7.x laravel getOriginal() can't bypass accessors Oct 20, 2020 at 17:27
  • 1
    This should be the correct answer Feb 9, 2022 at 6:12
  • @LucasM.Oliveira if we know this answer is correct we can share it on your social network in LinkedIn or etc, please. if you can do this help others to find the answer sooner. ;) Feb 18, 2022 at 17:05
3

I was running into an issue with Eloquent accessors and form model binding - by formatting an integer with money_format, the value was no longer being loaded into the form number input field.

The workaround I am using is to create an accessor with a different name:

public function getRevenueDollarsAttribute($value)
{
    return money_format('$%i', $this->revenue);
}

This provides me with an accessor without affecting the form model binding.

1

In my case for Laravel 7.15.0

public function getOriginalNameAttribute()
{
    return $this->attributes['name'];
}

// access it via
$user->originalName;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.